Search code examples
c#jsonunity-game-engineserializationjson.net

Unity Newtonsoft.Json serializing int default value as string


When I'm serializing any class that have int field inside, when it's value is 0, it serializes it as string instead of int.

I have a class:

[Serializable]
public class ResourceData
{
  public int Amount;
  public string Key;
}

When I serialize it in json string using Newtonsoft.Json

jsonSettings = new JsonSerializerSettings
{
  DefaultValueHandling = DefaultValueHandling.Include, 
};

var resourceData = new ResourceData();
string jsonString = JsonConvert.SerializeObject(resourceData, jsonSettings);

jsonString looks like this:

{
  "Amount": "0", // Instead of {"Amount": 0} I got {"Amount": "0"}. String instead of int
  "Key": ""
}

But it happens only with int value zero, if it anything else but zero it serializes properly. For example:

jsonSettings = new JsonSerializerSettings
{
  DefaultValueHandling = DefaultValueHandling.Include, 
};

var resourceData = new ResourceData();
resourceData.Amount = 5;
string jsonString = JsonConvert.SerializeObject(resourceData, jsonSettings);

// jsonString contains:
{
  "Amount": 5,
  "Key": ""
}

I need Amount to always be serialized as int, not string.

Thank you in advance.


Solution

  • You have really strange result that is not possible. When I test your code I got

    {"Amount":0,"Key":null}
    

    And from Newtonsoft documentation: " DefaultValueHandling.Include - Include members where the member value is the same as the member's default value when serializing objects. Included members are written to JSON. Has no effect when deserializing."

    It has nothing to do with with changing property values, it only includes or not includes the whole property INTO JSON STRING during serialazation.

    So if you select DefaultValueHandling.Ignore you will get

    {}
    

    for the first case and

    {"Amount":5}
    

    for resourceData.Amount = 5;

    And to have more stable result for another options I highly recomment you to add getters/setters to your class

    public class ResourceData
    {
      public int Amount {get; set;}
      public string Key {get; set;}
    }