question

AndrNobrega-1020 avatar image
0 Votes"
AndrNobrega-1020 asked ·

ASP.Net: isSpecified convention for JSON

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?

dotnet-aspnet-webapi
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YihuiSun-MSFT avatar image
0 Votes"
YihuiSun-MSFT answered ·

Hi @AndrNobrega-1020,

If you don't set the value of PersonSpecified, its value is false by default.
You can click on the link about: JSON Media-Type Formatter.
▶Edit◀

I find it strange that I don't need to set the value of "IdSpecified", "FirstNameSpecified" or "LastNameSpecified".
Complex objects will always have their respective "isSpecified" property set to false.

✦As I mentioned earlier, even if you do not make any assignments, the default value of the bool type will be set to false.
✦Do you want to not set the default value -false for the bool type?In this case, you can use a nullable value type.

  public bool? DetailsSpecified { get; set; }

However, if you do not manually assign the value this way, it will still set the default value to null.

Now I'm trying to add support for JSON input as well

✦Web API includes JsonMediaTypeFormatter class that handles JSON format.
✦Regarding the above mentioned link (JSON Media-Type Formatter):
JSON formatter can be configured in WebApiConfig class. You can customize JSON serialization using the JsonMediaTypeFormatter class.

If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
YihuiSun

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndrNobrega-1020 avatar image
0 Votes"
AndrNobrega-1020 answered ·

Hello @YihuiSun-MSFT, thanks for the reply.

I find it strange that I don't need to set the value of "IdSpecified", "FirstNameSpecified" or "LastNameSpecified". They will correctly show if their respective counterparts are in the JSON or not, without me having to explicitly declare it.

Is there a reason why this works for simple objects, but not for complex objects? I looked through the article you linked, but I didn't see anything about the IsSpecified convention.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AndrNobrega-1020,
I modified the previous answer, you can check it.

0 Votes 0 ·