std.json parsing issues
GrimMaple
grimmaple95 at gmail.com
Thu Nov 7 08:27:18 UTC 2019
On Wednesday, 6 November 2019 at 23:14:45 UTC, Jonathan Marler
wrote:
> On Wednesday, 6 November 2019 at 21:48:48 UTC, GrimMaple wrote:
>> [...]
>
> Looking at the json spec, it looks like it doesn't distinguish
> between integers and floating point values.
>
> As D is a native language, the distinction between integer and
> floating point values it semantically important. The
> operations on each have very different behavior.
>
> That being said, adding support in `std.json` to interpret both
> integers and floating points as floats should be trivial to
> implement. Or you could add your own function in your app.
>
> float asFloating(JSONValue value)
> {
> return (value.type == JSONType.integer) ?
> cast(float)value.integer : value.floating;
> }
>
> auto json = parseJSON(`{"num":123}`);
> writefln("num is: %s", json["num"].asFloating);
> json = parseJSON(`{"num":123.456}`);
> writefln("num is: %s", json["num"].asFloating);
Thank you for your input! The idea with additional function is
great, I always forget D can do that. As to adding this to
`std.json`, I'm both hands down to doing it myself, just unsure
if I should submit a bug first or if I can just PR it straight
away.
More information about the Digitalmars-d
mailing list