std.json dynamic initialization of JSONValue

David d at dav1d.de
Wed Dec 7 07:31:01 PST 2011


Am 06.12.2011 22:30, schrieb Kai Meyer:
> I posted this on D.learn, but got no responses. I'm hoping it's because
> I'm asking the wrong crowd.
>
> I'm finding std.json extremely well written, with one glaring exception.
>
> I can't seem to figure out how to do this:
>
> JSONValue root = JSONValue(null, JSON_TYPE.OBJECT);
> root.object["first_object"] = JSONValue(null, JSON_TYPE.OBJECT);
> root.object["first_string"] = JSONValue("first_string", JSON_TYPE.STRING);
>
> which would decode to:
>
> {"first_object":{},"first_string":"first_string"}
>
> What I end up having to do is:
> JSONValue root;
> root.type = JSON_TYPE.OBJECT;
> root.object["first_object"] = JSONValue();
> root.object["first_object"].type = JSON_TYPE.OBJECT;
> root.object["first_string"] = JSON_Value();
> root.object["first_string"].type = JSON_TYPE.STRING;
> root.object["first_string"].str = "first_string";
>
> That just feels like I'm doing it wrong. Is there a way to dynamically
> initialize a JSONValue struct? If I try to intialize the JSONValue
> object with anything other than simply null, or empty string, I either
> get a compile error or a segfault at run-time.
>
> root.object["first_object"] = JSONValue(null, JSON_TYPE.OBJECT);
>
> compile error:
> Error: overlapping initialization for integer
>
> root.object["first_string"] = JSONValue("first_string");
> run-time segfault.
>
> Any ideas?
That's the reason why I use libdjson (well the bug with dmd 2.053 made 
me switch, but I didn't switch back): 
https://256.makerslocal.org/wiki/Libdjson


More information about the Digitalmars-d mailing list