std.json cannot read an array floats back from file

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 2 14:15:41 PDT 2017


Yuri wrote:

> Hi there,
>
> consider the following simple use case:
>
>    import std.json;
>    float[] floats = [1,2,3];
>    JSONValue j = "{}".parseJSON;
>    j.object["floats"] = floats;
>    std.file.write("test.json", j.toString);
>    JSONValue jj = readText("test.json").parseJSON;
>    jj.object["floats"].array[1].floating.writeln;
>
> It is expected to print '2' in the console, however an exception is 
> thrown:
>
> std.json.JSONException@/build/ldc-I3nwWj/ldc-0.17.1/runtime/phobos/std/json.d(235): 
> JSONValue is not a floating type
>
> while the below works fine:
>
>    import std.json;
>    float[] floats = [1,2,3];
>    JSONValue j = "{}".parseJSON;
>    j.object["floats"] = floats;
>    j.object["floats"].array[1].floating.writeln;
>
> any pointers to what could be going wrong?

'cause what you got back is `JSON_TYPE.INTEGER`. dunno why it is there at 
all, as JSON has no "integers" per se, but it read as integer, and 
`.floating` failed type checking.

so, write your own wrapper that will convert INTEGER/UINTEGER/FLOAT to 
`double`. i think this is the best solution (if there can be "best 
solution" with std.json at all).


More information about the Digitalmars-d-learn mailing list