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 workaround for this? or any other .NET Json library that can handle unsigned int's?
*EDIT: code below; * It serializes fine, but when its deserializing it throws an error; Looks like it doesnt cater for the unsigned int from looking at the stack trace;
NewTonsoft.Json.JsonReaderException : {"JSON integer 18446744073709551615 is too large or small for an Int64."}
Value was either too large or too small for an Int64.
at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToInt64(String value, IFormatProvider provider)
at Newtonsoft.Json.JsonTextReader.ParseNumber() in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonTextReader.cs:line 1360
class Program
{
static void Main(string[] args)
{
string output = JsonConvert.SerializeObject(new ulong[] {ulong.MinValue, 20, 21, 22, ulong.MaxValue});
Console.WriteLine(output);
ulong[] array = JsonConvert.DeserializeObject<ulong[]>(output);
Console.WriteLine(array);
Console.ReadLine();
}
}
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 workaround for this? or any other .NET Json library that can handle unsigned int's?
*EDIT: code below; * It serializes fine, but when its deserializing it throws an error; Looks like it doesnt cater for the unsigned int from looking at the stack trace;
NewTonsoft.Json.JsonReaderException : {"JSON integer 18446744073709551615 is too large or small for an Int64."}
Value was either too large or too small for an Int64.
at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToInt64(String value, IFormatProvider provider)
at Newtonsoft.Json.JsonTextReader.ParseNumber() in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonTextReader.cs:line 1360
class Program
{
static void Main(string[] args)
{
string output = JsonConvert.SerializeObject(new ulong[] {ulong.MinValue, 20, 21, 22, ulong.MaxValue});
Console.WriteLine(output);
ulong[] array = JsonConvert.DeserializeObject<ulong[]>(output);
Console.WriteLine(array);
Console.ReadLine();
}
}
Share
Improve this question
edited Feb 20, 2012 at 8:31
svick
245k53 gold badges404 silver badges529 bronze badges
asked Feb 20, 2012 at 1:21
Ricky GummadiRicky Gummadi
5,2402 gold badges49 silver badges70 bronze badges
3
- What is the exact error you're getting? Can you also post the stack trace, along with relevant bits of your code? – svick Commented Feb 20, 2012 at 1:32
- It's probably your code. Why don't you post it? – Steve Wellens Commented Feb 20, 2012 at 1:32
- 1 Last but not least, I've found it useful to tell you that the DLL from net45 doesn't have this problem, whereas the one from portable-net45+wp80+win8+wpa81 have it... I changed my project references to target the net45 only, and the problem disappeared. But if your app is a wp one, you're stuck... – Loul G. Commented Nov 28, 2014 at 17:09
2 Answers
Reset to default 10ECMA-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.
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[]
.