Create an empty json object with std.json

userABCabc123 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 22 04:05: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;

read this:

https://issues.dlang.org/show_bug.cgi?id=15410

when you add the first key, the value will be set to 
JSON_TYPE.OBJECT

----
import std.json;

void main(string[] args)
{
     JSONValue json;
     json["first"] = 0;
     assert(json.type == JSON_TYPE.OBJECT);
}
----


More information about the Digitalmars-d-learn mailing list