Search code examples
c#jsondeserialization

JsonConvert.DeserializeObject return null for the object instead of the overflow integer itself


I have the following data object.

    public class myObject
        {
            public string user
            {
                get; set;
            }
    
            public List<available> available
            {
                get; set;
            }
    
        }
        public class available
        {
            public string name
            {
                get; set;
            }
            public int points
            {
                get; set;
            }
   }
        

If I use JsonConvert.DeserializeObject to deserialize the following data (overflowed points)

{
   "user":"12345",
   "available":[
      {
         "name":"test",
         "points":1234567891012345
      }
   ]
}

it returns null for available object

but if I have the following class and

public class available
        {
            public int points
            {
                get; set;
            }
            }

deserialize the following data, it returns 0 for points.

{
            "points": 1234567891012345,
}

Why did the first one returns null for the object?


Solution

  • First, suggestion is, please improve your question so we can help you answer accurately.

    Second I can see exception in this scenario from newton soft because you are using output bound value for integer.

    Newtonsoft.Json 13.0.2

    You should see an error, something like below:

    Unhandled exception. Newtonsoft.Json.JsonReaderException: 
    JSON integer 1234567891012345 is too large or small for an Int32. 
    Path 'available[0].points'```
    
    Use a data type for your needs appropriately.