Home » C# » How to Have Json.Net Deserialize Using Private Constructor and Private Setter

How to Have Json.Net Deserialize Using Private Constructor and Private Setter

You may have a private constructor for your class. This is often useful if you want the developer to use a constructor but still have your ORM to be able to create your entity. The problem is when serializing and deserializing will use the public constructor and will pass null on every parameters. To have the deserializer user the private constructor, like the ORM, you need to tell Json.Net that it can use private constructor.

 new JsonSerializerSettings
                    {
                        ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
                    }

The JsonSerializerSettings required is the ConstructorHandling which need to be set to AllowNonPublicDefaultConstructor.

Also, you may have public getter but private setter. Often the case when the property value must be set with private logic. However, when serializing and deserializing, these values will not be set. To allow Json.Net to use the private setter, you must set a new contract resolver.

Json.Net can have custom contract resolver. The trick is to tell to Json.Net that if the property has a private setter to use it.

new JsonSerializerSettings
                    {
                        ContractResolver = new PrivateResolver()
                    }
//.........
public class PrivateResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member
                                                 , MemberSerialization memberSerialization)
    {
        var prop = base.CreateProperty(member, memberSerialization);

        if (!prop.Writable)
        {
            var property = member as PropertyInfo;
            if (property != null)
            {
                var hasPrivateSetter = property.GetSetMethod(true) != null;
                prop.Writable = hasPrivateSetter;
            }
        }

        return prop;
    }
}

This is it.
DefaultContract
The CreateProperty method returns a JsonProperty which has some information about the serialization settings. As you can see in the image above, it’s possible to see if we have a get and set with the Readable and Writable property of the the JsonProperty. The only task we need to do is to set the Writable to true if this one is not yet set to deserialize (Writable) and if the property has a setter. This is done by using reflection and check if the property has a setting. This is done by using the PropertyInfo on the MemberInfo and use GetSetMethod.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

One Response so far.

  1. Aaron Passchier says:

    Phenomenal! Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.