Thread overview | |||||
---|---|---|---|---|---|
|
November 06, 2019 std.json parsing issues | ||||
---|---|---|---|---|
| ||||
Hello everyone! I'm fairly new to D and don't know much about its internals, but I've been playing around with std.json and found out a pretty weird behaviour in it. Let's say I have a JSON like this: { "Value": 123 }. When I parse it and try to writeln(json["Value"].floating) it throws a runtime error saying that "Value" is not "floating". I understand that it's being taken as an integer, but aren't integers subset of decimals? I would expect it to be able to convert to float no problem, but it's not the case. Does anyone know if this behaviour is intended or if it's a bug? |
November 06, 2019 Re: std.json parsing issues | ||||
---|---|---|---|---|
| ||||
Posted in reply to GrimMaple | On Wednesday, 6 November 2019 at 21:48:48 UTC, GrimMaple wrote:
> Hello everyone! I'm fairly new to D and don't know much about its internals, but I've been playing around with std.json and found out a pretty weird behaviour in it. Let's say I have a JSON like this: { "Value": 123 }. When I parse it and try to writeln(json["Value"].floating) it throws a runtime error saying that "Value" is not "floating". I understand that it's being taken as an integer, but aren't integers subset of decimals? I would expect it to be able to convert to float no problem, but it's not the case. Does anyone know if this behaviour is intended or if it's a bug?
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);
|
November 07, 2019 Re: std.json parsing issues | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Marler | 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.
|
Copyright © 1999-2021 by the D Language Foundation