[Development] JSON 64 bit int

Bo Thorsen bo at vikingsoft.eu
Wed Feb 11 12:12:33 CET 2015


Den 11-02-2015 kl. 11:58 skrev Giuseppe D'Angelo:
> On 11 February 2015 at 11:40, Bo Thorsen <bo at vikingsoft.eu> wrote:
>> {"i":1e33} gives a 0 if I convert the number to int (which is fair
>> enough). If I convert to variant, it gives me a double.
>
> Because it _is_ a double, given that JSON is a subset of Javascript,
> and numbers in Javascript are IEEE754 64bit floating point numbers.
> (And it's also a strict subset, given that JSON omits the handling of
> +/- infinity and NaN...). I'm not too happy with the fact that
> QJsonValue has "int" in its API...

I know this is the history of JSON, but it's no longer the reality. The 
use of JSON are all over the place now and they are not restricting 
themselves to Javascript numbers. You can find requests in the bug 
tracker for what I just asked about - JSON is no longer just about 
Javascript.

We have one of two choices here: Follow a restrictive schema that is no 
longer a description of how it's used. Or follow what users outside of 
the Javascript world would like to have.

It's so easy to say we just follow the standard. But I have two current 
projects where my customers say they send me a 64 bit database ID in a 
JSON value. Converting those through a double might work, but can you 
guarantee this? Yes, they do something that's wrong. But this is how I 
see JSON used these days. Restricting the JSON values to a set of types 
from one of the simplest typesystems found in programming languages is 
just silly IMHO.

If I say to my customers they have to send me another language because 
the Qt JSON library can't read 64 bit unsigned integers they say "Qt 
sucks, switch to another JSON library.". We can't win here.

Well, there's actually a third option: Give me the original QByteArray 
that the value comes from. If there was a way to get this, I can convert 
the value any way I want. Something like "QByteArray bytes() const" 
would allow for any kind of parsing on our side. I'd be fine with this 
as a workaround that would allow us to stay with the spec.

Bo Thorsen,
Director, Viking Software.

-- 
Viking Software
Qt and C++ developers for hire
http://www.vikingsoft.eu



More information about the Development mailing list