tony.hegyes
tony.hegyes

Reputation: 265

Json .NET Serialization - change the property value

Good day!

I am currently using the Newtonsoft Json Serializer through the following code:

        private string serializeAndIgnoreEmail(UserMembership obj)
        {
            var json = JsonConvert.SerializeObject(obj, Formatting.Indented,
                new JsonSerializerSettings() { ContractResolver = new DocumentIdContractResolver() });
            return json;
        }
        private class DocumentIdContractResolver : CamelCasePropertyNamesContractResolver
        {
            protected override List<MemberInfo> GetSerializableMembers(Type objectType)
            {
                return base.GetSerializableMembers(objectType).Where(o => o.Name != "Email").ToList();
            }
        }

Everytime I need to serialize an object I call the 'serializeAndIgnoreEmail' method. I now want to replace the content of each property with it's encrypted version and I don't know where to do this.

My guess would be to override a method in the 'DocumentIdContractResolver', but there are so many CreateBlahBlahBlah ones, that I find it very hard to work with them.

Is this the right approach, to continue modifying the ContractResolver or should I try something else?

Thank you!

Upvotes: 7

Views: 14480

Answers (2)

Matt Shams
Matt Shams

Reputation: 251

I have not done the exact same thing. But in my case for a web API project, I needed to serialize string names of enum values instead of their numeric values. I did a bit of research and realized that Json formatter converters are empty by default. So I added :

config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());

In your case, you need to write a custom JsonConverter and add it to the list of converters. You can find a similar example here:

Custom Json Converter

Upvotes: 1

Dave Van den Eynde
Dave Van den Eynde

Reputation: 17435

Calling SerializeObject does two things: create a tree of JSON tokens based on the object you specify, and serialize that into a string containing the JSON.

Your best bet would be to do the two steps separately: first ask JSON.NET to provide you with the tree of JSON tokens, then modify the values, then serialize them to JSON.

From the top of my head:

namespace JsonEncryptionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var obj = new
                {
                    To = "Some name",
                    Subject = "A Subject",
                    Content = "A content"
                };

            var jsonObject = JObject.FromObject(obj);

            // modify the values. Just doing something here to amuse you.
           var property = jsonObject.Property("Content");
           var value = (string) property.Value;
           property.Value = value.ToLower();

            var json = jsonObject.ToString();

            Console.WriteLine(json);
        }
    }
}

Upvotes: 8

Related Questions