On Fri, 18 Jul 2014 20:29:34 GMT, Pavel wrote:
I have a problem with creating a simple array with values using
vibe.data.json. Here's my code:Json j = Json.emptyObject; j.name = "Bert"; j.a = [1, 2, 3]; writefln("JSON: %s", j.toString());When I launch it I get:
JSON: {"a":"<rubbish here>","name":"Bert"}
Rubbish is likely those 3 values (1,2,3) each represented as a character.
PS: Maybe I was not attentive reading the docs, but I didn't find any sample on how to do that. I'm using vibe.d 0.7.20.
There are three possible ways to do that:
j.a = [Json(1), Json(2), Json(3)];or similarlyj.a = [1, 2, 3].map!(i => Json(i)).array;j.a = serializeToJson([1, 2, 3]);j.a = Json.emptyArray; j.a ~= 1; j.a ~= 2; j.a ~= 3;
The reason why it comes out as a corrupted string is that DMD is able to implicitly convert [1, 2, 3] to string (aka immutable(char)[]) and then chooses the opAssign overload taking a string instead of the one taking a Json[].