Search code examples
json.net

Newtonsoft Json.NET reference on Required and NullValueHandling settings


I'm not finding a good reference table explaining the exact serializing and deserializing behavior of Newtonsoft Json.NET for the various combinations of the Required and NullValueHandling settings. So, I will "ask" for them here and then post an answer shortly. For clarity, these are settings that can be set on the C# class definition, for example:

    public class Person
    {
        [Newtonsoft.Json.JsonProperty("firstname", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public string Firstname { get; set; }

        [Newtonsoft.Json.JsonProperty("enabled", Required = Newtonsoft.Json.Required.DisallowNull, 
            NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public bool? Enabled { get; set; } = true;
    }

The Required and NullValueHandling settings can also be set at the object level using, for example:

    [Newtonsoft.Json.JsonObject(ItemRequired=Newtonsoft.Json.Required.Always, ItemNullValueHandling=Newtonsoft.Json.NullValueHandling.Include)]
    public class Person
    {...}

Or in the case of NullValueHandling, it can also be set in the JsonSerializerSettings provided to the SerializeObject/DeserializeObject calls.

Anyway, how do those settings affect the behavior of the serializing and deserializing when the .Net object property is null and/or when the JSON text being deserialized either gives a null value or is completely missing?

I haven't found a good reference table already posted anywhere.


Solution

  • Based on my testing with Json.NET 13.0.1, this is the behavior for both serializing and deserializing:

    Required NullValueHandling Serialize: JSON result if .Net property is null Deserialize:result if field missing in JSON Deserialize: result if field is null in JSON
    Default Include included, but null property is default property is null
    Default Ignore not included in JSON property is default property is default
    AllowNull Include included, but null Exception property is null
    AllowNull Ignore not included in JSON Exception property is default
    Always Include Exception Exception Exception
    Always Ignore not included in JSON Exception Exception
    DisallowNull Include Exception property is default Exception
    DisallowNull Ignore not included in JSON property is default Exception

    A few notes about the results shown above:

    • "included, but null" means the JSON will look like "firstname":null
    • "property is default" means the .Net property's default value will remain unchanged. Of course in many cases that's null, but in the case of the example given in the question, the Enabled property defaults to true.
    • "property is null" means even if the .Net property has a non-null default value, it will get set to null.
    • "Exception" of course means an Exception will be thrown as a result of this scenario.

    For reference, if you do not specify these settings, the defaults are:

    • For Required: you guessed it, Default
    • For NullValueHandling: Include