Thread overview
JSONValue floating and 42
Apr 19, 2016
Andre
Apr 19, 2016
Edwin van Leeuwen
Apr 19, 2016
Andre
April 19, 2016
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
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
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é