Create an empty json object with std.json

Borislav Kosharov via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 22 15:01:48 PST 2016


On Friday, 22 January 2016 at 11:53:11 UTC, Andrea Fontana wrote:
> If you declare a JSONValue like this:
>
> JSONValue json;
>
> then:
>
> assert(json.type() == JSON_TYPE.NULL);
>
> Documentation at 
> https://dlang.org/phobos/std_json.html#.JSONValue.type.2 
> suggests not to change type but to assign a new value instead.
>
> My problem is: how can I assign an empty object like {}?
>
> The only way i found is using that deprecated method:
> json.type = JSON_TYPE.OBJECT;
>
> or
>
> json = `{}`.parseJSON;
>
> Please notice that to init as array this works:
> json = JSONValue[].init;

What you can do is use the JSONValue ctor and pass it an empty 
associative array

     auto json = JSONValue(string[string].init); //or 
JSONValue((JSONValue[string]).init)

It's a little verbose but gets the job done without using 
deprecated things or parseJSON


More information about the Digitalmars-d-learn mailing list