Neeraj Kumar Gupta
Neeraj Kumar Gupta

Reputation: 2363

Serialize the custom attributes along with class properties in C# on .NET?

I have a Person class with custom attribute over few properties, I want to serialize this class object using Newtonsoft JSON.NET features. My custom attribute should also get serialized into the JSON.

Do we have anything built-in in Newtonsoft JSON.NET for this?

public class Person
{
    public Int32 Id { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
        
    [Encrypted(key="PersonalData")]
    public String Address { get; set; }

    [Encrypted(key="PersonalData")]
    public String Phone { get; set; }

    [Encrypted(key="PersonalData")]
    public String IdentityDetails{ get; set; }
}

When the JSON string gets exported through serialization, the applied custom attributes over the properties should also get exported as part of the JSON, so that by looking at the JSON data, we can identify that those property value are encrypted. So that other program where we are sending the JSON string can take the action accordingly for those property value, my class is not fixed, we can send any class JSON string to other program that is responsible to read the JSON and process that.

Upvotes: 0

Views: 120

Answers (0)

Related Questions