[Done] How to stop a double getting mangled in JSON?

Just starting out? Need help? Post your questions and find answers here.
User avatar
the.weavster
Addict
Addict
Posts: 1581
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

[Done] How to stop a double getting mangled in JSON?

Post by the.weavster »

How do I stop a double getting mangled in JSON?
Here's a very simple example:

Code: Select all

x.d = 140631120846632
Debug x

If CreateJSON(0)
  y = SetJSONObject(JSONValue(0))
  SetJSONDouble(AddJSONMember(y, "x"), x)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
The output is:
140631120846632.0
{
"x": 1.406311208e+14
}
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: How to stop a double getting mangled in JSON?

Post by Demivec »

I can confirm the undesirable output. It can be fully represented as a string using StrD() as a converter but not as a number. I know a string is not what is wanted though.

Regarding numbers the standard linked to in the PureBasic Help says this:
RFC 7159 JSON March 2014 wrote:This specification allows implementations to set limits on the range
and precision of numbers accepted. Since software that implements
IEEE 754-2008 binary64 (double precision) numbers [IEEE754] is
generally available and widely used, good interoperability can be
achieved by implementations that expect no more precision or range
than these provide, in the sense that implementations will
approximate JSON numbers within the expected precision. A JSON
number such as 1E400 or 3.141592653589793238462643383279 may indicate
potential interoperability problems, since it suggests that the
software that created it expects receiving software to have greater
capabilities for numeric magnitude and precision than is widely
available.
While it is implementation dependent the implementation should realistically be one that is able to include double precision values fully. This appears to not be the case here, only float precision appears to be implemented.

I think this would fall under a feature request to update the representation of numbers to include full double precision floats.

I've made a feature request.
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: [Done] How to stop a double getting mangled in JSON?

Post by Fred »

Fixed.
Post Reply