www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - JSONValue floating and 42

reply Andre <andre s-e-a-p.de> writes:
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é
Apr 19 2016
parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
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
Apr 19 2016
parent Andre <andre s-e-a-p.de> writes:
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é
Apr 19 2016