Re: std.json cannot read an array floats back from file
On Monday, 3 July 2017 at 14:04:47 UTC, ketmar wrote: Yuri wrote: On Sunday, 2 July 2017 at 21:15:41 UTC, ketmar wrote: [...] I share your sentiment in relation to std.json, ketmar. On a side note, what would be a better way to serialize/deserialize objects in D if std.json does not cut it? It does not have to be JSON serialization although would be preferred. it depends of the types of your objects. simple json-like (de)serializer for objects with fixed layout can be done in very small amount of code[0]. that's what i am using (and it can *read* json into structs too, i'm actually using it to parse some jsons -- idgames API replies, for example). [0] http://repo.or.cz/iv.d.git/blob_plain/HEAD:/txtser.d Thanks, ketmar, I'll have a look into the code, the objects I am dealing with are not particularly complex, that might work well enough.
Re: std.json cannot read an array floats back from file
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.
Re: std.json cannot read an array floats back from file
On Sunday, 2 July 2017 at 21:15:41 UTC, ketmar wrote: 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). I share your sentiment in relation to std.json, ketmar. On a side note, what would be a better way to serialize/deserialize objects in D if std.json does not cut it? It does not have to be JSON serialization although would be preferred.
Re: std.json cannot read an array floats back from file
On Sunday, 2 July 2017 at 21:12:48 UTC, Adam D. Ruppe wrote: On Sunday, 2 July 2017 at 21:07:40 UTC, Yuri wrote: 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 I think it just read the json string of "1" as being an integer instead of a float, so the reader thought it was integral instead of floating. It should prolly just transparently auto-convert, but it doesn't seem to. Try accessing the int instead and see waht happens. 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.
std.json cannot read an array floats back from file
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?