Json.NET crashes when serializing unsigned integer (ulong) array

后端 未结 2 1971
無奈伤痛
無奈伤痛 2020-12-10 18:12

Getting a parser error when trying to serialize a ulong array, looks like the Json.NET library isnt checking if the integer is signed or unsigned; any one know of a workaro

相关标签:
2条回答
  • 2020-12-10 18:33

    ECMA-262, the standard on which JSON is based, specifies in section 4.3.19 that number values are IEEE double-precision floating point values, commonly seen as the "double" type in C-like languages. This encoding is not sufficiently precise to represent all possible values of 64 bit integers.

    Therefore, encoding 64 bit integers (signed or otherwise) in JSON may lead to a loss in precision if it passes through any code which processes it in keeping with the standard. As seen in JSON.net, it might also break code which does not correctly implement the standard, but rather assumes that people won't try to do failure-prone things.

    0 讨论(0)
  • 2020-12-10 18:34

    You're right, JSON.Net doesn't handle values larger than long.MaxValue in this case.

    I didn't find any way to modify that behavior, except by modifying the source code of the library. As a workaround, you could deserialize it as decimal[] and then convert that into ulong[].

    0 讨论(0)
提交回复
热议问题