std.json cannot read an array floats back from file

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 3 06:34:50 PDT 2017


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


More information about the Digitalmars-d-learn mailing list