users@jsonb-spec.java.net

[jsonb-spec users] Number format to match ECMA-262 (JavaScript)

From: Manger, James <James.H.Manger_at_team.telstra.com>
Date: Wed, 30 Nov 2016 02:49:42 +0000

It would be useful if JSONB<http://json-b.net/> could serialize numbers into JSON in exactly the same way that ECMA-262 (JavaScript) specifies - ideally by default, but as an optional mode of JSONB if necessary.

Consider the three numbers: 1e-6, 1e-7, and 1234567890123456789.
Java's Double.toString(double) (and I believe the JSON-B draft spec) prints these as:
  1.0E-6, 1.0E-7, and 1.23456789012345677E18
whereas JavaScript's JSON.stringify() prints them as:
  0.000001, 1e-7, and 1234567890123456800

Either form (and many others) is valid JSON for these values so Jsonb.fromJson() needs to accept them all (as I'm sure it does already).
But when creating JSON it would be better to have a consistent canonical form created by Jsonb.toJson() - consistent with other languages such as JavaScript.

ECMA-262 (7th edition, 2016) specifies its number format for JSON in section 7.1.12.1 "ToString Applied to the Number Type". The ECMA-262 rules are not quite as simple to read as those for Double.toString(double), but JSON's heritage is ECMA-262 so JSONB should follow its rules here.
https://www.ecma-international.org/ecma-262/7.0/index.html#sec-tostring-applied-to-the-number-type


--
James Manger