Detect if deserialized object is missing a field with the JsonConvert class in Json.NET
Solution 1
The Json.Net serializer has a MissingMemberHandling
setting which you can set to Error
. (The default is Ignore
.) This will cause the serializer to throw a JsonSerializationException
during deserialization whenever it encounters a JSON property for which there is no corresponding property in the target class.
static void Main(string[] args)
{
try
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MissingMemberHandling = MissingMemberHandling.Error;
var goodObj = JsonConvert.DeserializeObject<MyJsonObjView>(correctData, settings);
System.Console.Out.WriteLine(goodObj.MyJsonInt.ToString());
var badObj = JsonConvert.DeserializeObject<MyJsonObjView>(wrongData, settings);
System.Console.Out.WriteLine(badObj.MyJsonInt.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
}
Result:
42
JsonSerializationException: Could not find member 'SomeOtherProperty' on object
of type 'MyJsonObjView'. Path 'SomeOtherProperty', line 3, position 33.
See: MissingMemberHandling setting.
Solution 2
Just add [JsonProperty(Required = Required.Always)]
to the required properties and it'll throw exception if the property is not there while deserializing.
[JsonProperty(Required = Required.Always)]
public int MyJsonInt { get; set; }
Solution 3
Put the following attribute on required properties:
[DataMember(IsRequired = true)]
If the member is not present, it will throw a Newtonsoft.Json.JsonSerializationException.
As Brian suggested below, you will also need this attribute on your class:
[DataContract]
Solution 4
Just define your members in your definition class with a question mark '?' int?
:
private class MyJsonObjView
{
[JsonProperty("MyJsonInt")]
public int? MyJsonInt { get; set; }
}
When it is not initialized, it will just be null
, otherwise it will be a valid value. This allows you to have settings optional and evaluate them on a per-setting basis.
Solution 5
As @dbc tells in comments:
- At deserialization:
If your Model has a property that your JSON does not, and you want that to be an error, use
[JsonProperty(Required = Required.Always)]
.
- At serialization:
If your JSON has a property that your Model does not, and you want that to be an error, use
MissingMemberHandling = MissingMemberHandling.Error
.
also using [DataMember(IsRequired = true)]
for error at deserialization is true when proeprty type is nullable.
DubiousPusher
I program tools for video games. Currently I'm working at Microsoft Studios in occasionally sunny Redmond WA. I do like Pina Coladas but I prefer whiskey. Getting caught in the rain is a bit too routine around here.
Updated on November 11, 2020Comments
-
DubiousPusher over 3 years
I'm trying to deserialize some JSON objects using Json.NET. I've found however that when I deserialize an object that doesn't have the properties I'm looking for that no error is thrown up but a default value is returned for the properties when I access them. It's important that I'm able to detect when I've deserialized the wrong type of object. Example code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace Json_Fail_Test { class Program { [JsonObject(MemberSerialization.OptOut)] private class MyJsonObjView { [JsonProperty("MyJsonInt")] public int MyJsonInt { get; set; } } const string correctData = @" { 'MyJsonInt': 42 }"; const string wrongData = @" { 'SomeOtherProperty': 'fbe8c20b' }"; static void Main(string[] args) { var goodObj = JsonConvert.DeserializeObject<MyJsonObjView>(correctData); System.Console.Out.WriteLine(goodObj.MyJsonInt.ToString()); var badObj = JsonConvert.DeserializeObject<MyJsonObjView>(wrongData); System.Console.Out.WriteLine(badObj.MyJsonInt.ToString()); } } }
The output of this program is: 42 0
I would prefer an exception be thrown to failing silently. Short of that is there a way to detect if the serialization failed to find a parameter?
I know I can parse the data with a Json object and then check for the parameter with a key value lookup but the codebase I'm in uses the pattern above and I'd like keep that consistent if it's possible.
-
DubiousPusher over 10 yearsAre you sure about this? I tried adding the attribute and no exception is given. And the program still prints 0.
-
DubiousPusher over 10 yearsI actually found the settings class digging through the documentation but I must have skimmed over that property. I'm kind of a crap speed reader. Thanks so much; this is perfect.
-
Brian Rogers over 10 yearsThis solution will only work if you have
[DataContract]
on your class AND you do NOT have[JsonProperty]
on the member. (JsonProperty
overridesDataMember
.) You could, however, set theRequired
parameter on the[JsonProperty]
attribute toRequired.Always
to accomplish the same thing. -
Anoop Chandrika HarisudhanNair over 9 years@BrianRogers Thank you. It helped me alot. Do have any link to share on further studying JSON parsing, esp the missing and Null value handling issues?
-
Alu over 8 yearsI have a quite similar problem. If one member of the target class is missing in the JSON, the deserializer will ignore it and set it to null. Even if the MissingMember setting is set to error.
-
Alu over 8 yearsAlready found a solution. Used attributes to mark my members as required.
-
Alu over 8 yearsThe example would be: deserializing an empty json string "{}" into any type. It will set all values in the type to null. But I want an exception if the type doesnt match the json.
-
dbc over 4 yearsThis solves a complimentary problem. If your model has a property that your JSON does not, and you want that to be an error, use
[JsonProperty(Required = Required.Always)]
. See: JsonPropertyAttribute required. If your JSON has a property that your model does not, and you want that to be an error, useMissingMemberHandling = MissingMemberHandling.Error
. -
Andrew T Finnell almost 4 yearsThis cannot tell the different between when the property is missing or set to null in the JSON.
-
afruzan over 3 years@DubiousPusher properties should be nullable. then it will work.