std.json cannot read an array floats back from file
Yuri via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jul 3 08:57:58 PDT 2017
On Monday, 3 July 2017 at 13:34:50 UTC, Adam D. Ruppe wrote:
> On Monday, 3 July 2017 at 13:26:52 UTC, Yuri wrote:
>> Yes, when accessing .integer instead of .floating then it
>> works, unfortunately that is not suitable for the task at
>> hand, it has to be a float.
>
> Just write a helper function that casts it yourself:
>
> double numeric(JSONValue v) {
> if(v.type == JSON_VALUE.FLOAT)
> return v.floating;
> else if(v.type == JSON_VALUE.INTEGER)
> return v.integer;
> else if(v.type == JSON_VALUE.UINTEGER) // I think it has
> this too
> return v.uinteger;
> throw new Exception("not a numeric type, instead: " ~
> to!string(v.type));
> }
>
>
> and then you should be able to do
>
> jj.object["floats"].array[1].numeric.writeln;
>
> and have it return float regardless of if it is 1 or 1.0
Thanks Adam, that will work for me. I wish though there was no
need for jumping these hoops in a standard language lib but I
guess it would be a topic for another discussion.
More information about the Digitalmars-d-learn
mailing list