JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.84k stars 3.26k forks source link

Make JSON number serialization compatible with ES6 (and V8) #789

Open cyberphone opened 8 years ago

cyberphone commented 8 years ago

After looking into the code it seems that numbers do not follow ES6. This is usually not a problem but there's a new use-case which may motivate an update:

https://github.com/Microsoft/ChakraCore/issues/149

Unfortunately this is an non-trivial thing: https://github.com/cyberphone/openkeystore/blob/master/library/src/org/webpki/json/JSONObjectWriter.java#L147

JamesNK commented 8 years ago

Neither of those links tell me anything. Where is the ES6 number specification? And give an example of a number that is wrong and what the correct number is.

cyberphone commented 8 years ago

@JamesNK Here is the number specification: 7.1.12.1 in http://www.ecma-international.org/ecma-262/6.0/ECMA-262.pdf

I don't have a JSON.net example but double.toString() wasn't designed for ES6 compatibility so the difference in formatting is likely to be rather big.

 Double d = 0.00005;
 Console.Write(d);

The above returns 5E-05 while ES6 mandates 0.00005

cyberphone commented 8 years ago

Related: https://github.com/cyberphone/jsondotnet

JamesNK commented 8 years ago

I'll wait until MS fixes this in DCJS and then reuse it

https://github.com/dotnet/corefx/issues/5647

cyberphone commented 8 years ago

@JamesNK Sounds like a reasonable solution. I just hope it won't take forever :-)

cyberphone commented 6 years ago

Now it is available: https://github.com/cyberphone/json-canonicalization/tree/master/dotnet/es6numberserialization

Finally making clear text signatures using Json.NET fully realistic! https://github.com/cyberphone/json-canonicalization/tree/master/dotnet/json.net.sign#clear-text-signature-sample-solution

Unfortunately there is a bug in .NET with respect to parsing floating point numbers: https://github.com/dotnet/coreclr/issues/17467