RejectedSoftware Forums

Sign up

Json expected double, got int

I just ran into a problem with json serialization.

When exporting data to be consumed by vibe.d, my converter (not written
in D) can output floating point numbers like 3.0 as just 3. This results
in vibe typing this as an integer.

However, it's still also a valid double. Why doesn't get!double work on
this? Shouldn't it? I don't want to use to, because strings are not
valid in this location, and one could theoretically pass a string of "3"
or "3.0".

What I ended up doing:

if(x.type == Json.Type.float_) result = x.get!double;
else result = x.get!long;

Surely, this must be a common issue? Json grammar (and indeed javascript
itself) doesn't distinguish between integers and floating points.

Indeed, the Json deserializer correctly handles this as a special case:
https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/data/json.d#L1628

If it's just that "nobody has done it yet", I can create a PR.

-Steve

Re: Json expected double, got int

On 8/8/16 4:23 PM, Steven Schveighoffer wrote:

Indeed, the Json deserializer correctly handles this as a special case:
https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/data/json.d#L1628

I'll note that this is actually wrong in the case of BigInt. A float or
double could much more appropriately handle the BigInt value than an
intermediate long.

-Steve