Search code examples
json.net.net-corejson-deserialization

Deserialization of ulong failing for .NET Core


I'm doing a very simple deserialization of ulong:

    static void Main(string[] args)
    {
        try
        {
            var data = ulong.MaxValue;
            var serialized = JsonConvert.SerializeObject(data);
            var res = JsonConvert.DeserializeObject<ulong>(serialized);
            Console.WriteLine(res);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        Console.ReadKey();
    }

In a normal console application, this works fine. But when doing this in a .NET Core Console Application, it fails with the following error:

JSON integer 18446744073709551615 is too large or small for an Int64. Path '', line 1, position 20.

It seems to me that this is trying to convert to a long instead of a ulong. What is the matter here? Is this a bug in JSON .NET or .NET Core?


Solution

  • It was a bug in Newtonsoft.Json that will be fixed started from 9.0.2 (related issue).

    I have checked your code in my project and after adding direct referenced to "Newtonsoft.Json": "9.0.2-beta001" in project.json the problem is gone.