1

I'm trying to deserialize some Json objects using Json.NET. I'd like to be able to detect if I have a member in my class missing from the Json properties for that object. For instance, I have a class which looks like this :

public class MyClass
{
    public int n;
    public bool b;
    public string s;
}

And a Json which looks like this

{"n":1,"b":true}

so it's missing the "s" property. When I try to deserialize that, members which are not in the Json will have default value. So "s" will be equal to null. Fair enough, but is it possible to detect that when I'm deserializing ? In substance, I want to do pretty much the opposite of this Stackoverflow post

But in my case, the MissingMemberHandling setting seems to do nothing, sadly.

2 Answers2

1

Json.Net provides a way to achieve that.

You can set an attribute on the property in your Model class. and if that property is not available in JSON it'll throw an exception.

Here is the Example

Model

public class Videogame
{
    [JsonProperty(Required = Required.Always)]
    public string Name { get; set; }

    [JsonProperty(Required = Required.AllowNull)]
    public DateTime? ReleaseDate { get; set; }
}

Test Code

string json = @"{
                    'Name': 'Starcraft III'
                }";

Videogame starcraft = JsonConvert.DeserializeObject<Videogame>(json);

You can read more about this here

Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
  • I think this is what I was looking for. But actually, in my case, I want to check every field, so it would be inconvenient to set this attribute on every field of my class. It's probably a better approach to get every field name by reflection and to compare them to every json property name. Thanks a lot anyway, this is the best answer ;) – AlexKhundar Apr 17 '19 at 14:18
0

This is what i can think of at the moment.

public void CheckData()
{
    if(n == null)
    {
       Console.Write("n is null");
    }
    if (b == null)
    {
       Console.Write("b is null");
    }
    if (s == null)
    {
        Console.Write("s is null");
    }
}

Run a method like that after json maps to the object to see if there were any still null.

Or you can try something like this

var fields = typeof(MyClass).GetFields();

            foreach (var field in fields)
            {
                if(!json.ContainsKey(field.Name))
                {
                    Console.Write($"{field.Name} is missing");
                }
            }
JayHandle
  • 176
  • 7
  • This will not work because `n` and `b` are not nullable value types – Aleks Andreev Apr 16 '19 at 16:22
  • you can make them nullable values by putting "?" in front of the type. ex{ int? n} – JayHandle Apr 16 '19 at 16:27
  • Ok but let's say `s` could be null in the program. I'd like to be able to detect a difference between the following Json `{"n":1,"b":true}` and `{"n":1,"b":true,"s":null}` – AlexKhundar Apr 16 '19 at 16:33
  • If that is the case, don't check if "s" is null. That way even if the json doesn't provide an "s", it wont really matter. If you dont like this method, you can check if the json contains all the keys you want. For ex: json.ContainKey("s"). – JayHandle Apr 16 '19 at 16:43
  • I modified my answer on the top to show you what i mean. – JayHandle Apr 16 '19 at 16:52
  • @JayHandle yes, you led me to the right approach with your 2nd piece of code. Get all the fields name by reflection and compare them to the json properties is the best I can do in my case – AlexKhundar Apr 17 '19 at 14:20
  • No problem! Glad i could help. Happy coding! – JayHandle Apr 18 '19 at 02:52