Thread overview | |||||
---|---|---|---|---|---|
|
April 19, 2016 JSONValue floating and 42 | ||||
---|---|---|---|---|
| ||||
Hi, I have to deal with a JSON like this [{"value":42},{"value":1e+100}] Value is a floating number, but the providing system does not write 42 as 42.00. While trying to get the value with .floating, I get an exception because 42 is not a floating value. I try to understand what is the issue here. -> Does the system violates the JSON Standard, and it should deliver 42.00? -> .floating should be enhanced to handle 42? -> I need to analyze every value whether it is a floating or an integer? Kind regards André |
April 19, 2016 Re: JSONValue floating and 42 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre | On Tuesday, 19 April 2016 at 13:44:08 UTC, Andre wrote:
> -> I need to analyze every value whether it is a floating or an integer?
This is the correct option. Something like:
double f;
if (j["value"].type == JSON_TYPE.INTEGER)
f = j["value"].integer.to!float;
else
f = j["value"].floating;
There are also a number of libraries available that make dealing with json a bit easier:
code.dlang.org/search?q=json
|
April 19, 2016 Re: JSONValue floating and 42 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Edwin van Leeuwen | On Tuesday, 19 April 2016 at 13:58:05 UTC, Edwin van Leeuwen wrote:
> On Tuesday, 19 April 2016 at 13:44:08 UTC, Andre wrote:
>> -> I need to analyze every value whether it is a floating or an integer?
>
> This is the correct option. Something like:
>
> double f;
> if (j["value"].type == JSON_TYPE.INTEGER)
> f = j["value"].integer.to!float;
> else
> f = j["value"].floating;
>
> There are also a number of libraries available that make dealing with json a bit easier:
> code.dlang.org/search?q=json
I will do so. Thanks.
Kind regards
André
|
Copyright © 1999-2021 by the D Language Foundation