2

I have a C# Application.

I have a class that is generated from an xsd. The class looks as below

public class Transaction
{
    public bool amountSpecified {get; set;}

    public double amount {get; set;}
}

If you notice in the class above, along with the property amount, the generator has also generated a property called amountSpecified.

I know that the properties with suffix "Specified" are required for all non-nullable field/property, because this is the requirement of XML Serializer as mentioned in this [article][1].

However I only use JSON serialization and deserialization(with JSON.NET), do I still need those fields with "Specified" suffix? If I remove them should I make my fields/properties nullable as shown below?

double? amount;

My question being is all of this internally handled by JSON.Net? Can I safely remove all the fields with suffix "specified" and not make my fields nullable?

I would be very glad if someone can point me in the right direction. Thanks in Advance.

HelpMatters
  • 1,299
  • 1
  • 13
  • 32

1 Answers1

1

As discussed since 2008, they fixed it to support nullable type. Also I tried with this code

using System;
using Newtonsoft.Json;

namespace TestJson
{
    class Test {
        public double? amount { get; set; }
    }

    class MainClass
    {
        public static void Main(string[] args)
        {
            string jsonStr = JsonConvert.SerializeObject(new Test());
            string jsonStr2 = JsonConvert.SerializeObject(new Test { amount = 5 } );
            Console.WriteLine(jsonStr);
            Console.WriteLine(jsonStr2);
            Console.ReadLine();
        }
    }
}

It works just fine:

{"amount":null}
{"amount":5.0}

And the properties with Specified suffix are not required.

tia
  • 9,518
  • 1
  • 30
  • 44
  • I understand. My question was can I choose not to use even Nullable type and have my property look something like this public double amount {get; set;} From your answer this is a No! I must use "?" Nullable. Am I right? I tried by removing "?" the output was follows. {"amount":0.0} {"amount":5.0} I don't want the Json.Net to serialize if the amount is null. Is it possible? – HelpMatters Sep 01 '14 at 15:04
  • It would be clearer if you provide some example of how you want it to be. You mean you want JSON.NET to serialise `new Test { amount=0 }` as `{"amount":null}`? – tia Sep 01 '14 at 15:07
  • Sure. I want JSON.NET to serialize new Test() as { } Since amount is null it should not be serialized at all. – HelpMatters Sep 01 '14 at 15:10
  • JSON.NET will serialise every properties by default, but if you want to skip property with default values, you can use `DefaultValueHandling` option http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_NullValueHandling.htm – tia Sep 01 '14 at 15:22
  • Sorry if I was wee bit unclear before. I just wanted to understand if the nullable "?" is a requirement for JSON.NET to work correctly. How to handle if the value is null is specified here http://stackoverflow.com/questions/6507889/how-to-ignore-a-property-in-class-if-it-is-null-using-json-net I think I got my answer. Thanks – HelpMatters Sep 01 '14 at 15:37
  • So the short answer for the question I have pasted in this page is the fields and properties with the suffix "Specified" is not required right? Could you just incorporate the same in your answer. I can upvote and mark your answer as accepted. – HelpMatters Sep 01 '14 at 15:40