Live data from Hacker News

What even is a JSON number?

blog.trl.sn

1–10 of 151 posts

Re: What even is a JSON number?

#2
> I-JSON messages SHOULD NOT include numbers that express greater magnitude or precision than an IEEE 754 double precision number provides

I'm confused by this.

What is the precision of 0.1, relative to IEEE 754?

If I read it correctly, that statement is saying:

  json_number_precision(json_number) 
^ How do I calculate these values?

Re: What even is a JSON number?

#4

I think the description for Go is inaccurate/incomplete. You can call this function to instruct the decoder to leave numbers in unparsed string form: https://pkg.go.dev/encoding/json#Decoder.UseNumber That allows you to capture/forward numbers without any loss of precision.

I have added this note, thanks! In the blog I am mostly trying to show the behavior you get using the (maybe defacto) stdlib with its default configuration, but this is useful data to call out.

Re: What even is a JSON number?

#6
Since JSON is so widely used it should be modified to support more types - Mongo DB's Extended JSON supports all the BSON (Binary) types:

    Array
    Binary
    Date
    Decimal128
    Document
    Double
    Int32
    Int64
    MaxKey
    MinKey
    ObjectId
    Regular Expression
    Timestamp
https://www.mongodb.com/docs/manual/reference/mongodb-extend...

Re: What even is a JSON number?

#7

> I-JSON messages SHOULD NOT include numbers that express greater magnitude or precision than an IEEE 754 double precision number provides I'm confused by this. What is the precision of 0.1, relative to IEEE 754? If I read it correctly, that statement is saying: json_number_precision(json_number) ^ How do I calculate these values?

I think the spec just means, assume IEEE 754. In the case of 0.1, which cannot be represented exactly, software should assume that `0.1` will be represented as `0.100000000000000005551115123126`. Depending on `0.1` being parsed as the exact value `0.1` is not widely interoperable.

Re: What even is a JSON number?

#8
post #6

Since JSON is so widely used it should be modified to support more types - Mongo DB's Extended JSON supports all the BSON (Binary) types: Array Binary Date Decimal128 Document Double Int32 Int64 MaxKey MinKey ObjectId Regular Expression Timestamp https://www.mongodb.com/docs/manual/reference/mongodb-extend...

JS is likely to get a hook to be able to handle serialization/deserialization of such values without swapping out the entire implementation[1]. Native support for these types, without additional code or configuration, would likely break the Internet badly, so is unlikely to happen unfortunately.

1: https://github.com/tc39/proposal-json-parse-with-source

Re: What even is a JSON number?

#9

> I-JSON messages SHOULD NOT include numbers that express greater magnitude or precision than an IEEE 754 double precision number provides I'm confused by this. What is the precision of 0.1, relative to IEEE 754? If I read it correctly, that statement is saying: json_number_precision(json_number) ^ How do I calculate these values?

Relatedly, what about integers like 9007199254740995. Is that a legal integer since it rounds to 9007199254740996?

It does seem unclear what it means to exceed precision (given rounding is such an expected part of the way we use these numbers). Magnitude feels easier as at least you definitely run out of bits in the exponent.

Post reply on HN