Suppose I have the following JSON:
{
"name": "Jim",
"age": 20
}
And I deserialise it into the following C# object:
public class Person
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("age")]
public int? Age { get; set; }
[JsonProperty("height")]
public int? Height { get; set; }
}
Is there any way I can determine which properties were included in the the original JSON, and which were omitted?
In this example all my properties are nullable, the JSON didn't include the height
property, so my C# object will have end up with a null
Height
.
However it's also possible that a user could simply provide null
as the height, e.g.
{
"name": "Jim",
"age": 20,
"height": null
}
So my question is: Is it possible for me to determine if the value was provided but null
, or not provided and therefore defaulting to null
. Is there some meta data available somewhere/somehow that gives me this information?
This is used in an ApiController, so the deserialization is done by a Formatter automatically, but here is my current formatter setup:
private static void AddFormatter(HttpConfiguration config)
{
var formatter = config.Formatters.JsonFormatter;
formatter.SerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
TypeNameHandling = TypeNameHandling.None
};
}