I have an ASP.Net webservice, that uses XML as the input and output format for its API.
I am using the PropertySpecified convention. If, for example, my XML contains a <FirstName> tag, my POCO will contain that tag's value, and the POCO's FirstNameSpecified property will be "true".
Now I'm trying to add support for JSON input as well, but there seems to be a problem with the Specified properties.
I added the following formatter to WebApiConfig.cs:
config.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
JSON objects will be serialized, and the values of the explicitely specified fields will be correctly mapped to my POCO.
The "specified" properties, however, only work correctly for simple types (strings, integers, etc). Complex objects will always have their respective "isSpecified" property set to false.
For example, if I make a PUT call, with the following JSON in the request body:
{
"ID": "abc123",
"Person": {
"FirstName": "John",
"LastName": "Doe"
}}
... my POCO will have the following properties:
ID = "abc123";
IDSpecified = true;
Person = Person;
PersonSpecified = false;
Person.FirstName = "John";
Person.FirstNameSpecified = true;
Person.LastName = "Doe";
Person.LastNameSpecified = true;
Note that this only seems to happen when serializing JSON in ASP.Net, through the default JsonMediaTypeFormatter, that kicks in before your request reaches the controller. If you serialize JSON into an object anywhere else, using JsonConvert, the Specified properties are set as expected (https://dotnetfiddle.net/Iyry36)
Is there some parameter I'm missing for the JSON formatter, or am I doing something wrong?