www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create an empty json object with std.json

reply Andrea Fontana <nospam example.com> writes:
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;
Jan 22 2016
next sibling parent reply userABCabc123 <userABCabc123 skdhf.ri> writes:
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); } ----
Jan 22 2016
parent reply Andrea Fontana <nospam example.com> writes:
On Friday, 22 January 2016 at 12:05:48 UTC, userABCabc123 wrote:
 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);
 }
 ----
That's right, but I need an empty object, without any key set!
Jan 22 2016
parent reply userABCabc123 <userABCabc123 skdf.gz> writes:
On Friday, 22 January 2016 at 12:54:38 UTC, Andrea Fontana wrote:
 On Friday, 22 January 2016 at 12:05:48 UTC, userABCabc123 wrote:
 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);
 }
 ----
That's right, but I need an empty object, without any key set!
But soon or later you'll need to add values to your object so just imagine it's already an object, even if it will only become one when you'll start to add some values. I don't really get your problem here, as in the first message you also start with an empty json.
Jan 22 2016
parent reply Andrea Fontana <nospam example.com> writes:
On Friday, 22 January 2016 at 16:45:22 UTC, userABCabc123 wrote:
 But soon or later you'll need to add values to your object so 
 just imagine it's already an object, even if it will only 
 become one when you'll start to add some values.
You're wrong, I need an empty object for an API call! Moreover "{}" it's a valid json object and there's no easy way to create it using std.json.
Jan 22 2016
parent reply userABCabc123 <userABCabc123 skdf.gz> writes:
On Friday, 22 January 2016 at 16:58:51 UTC, Andrea Fontana wrote:
 On Friday, 22 January 2016 at 16:45:22 UTC, userABCabc123 wrote:
 But soon or later you'll need to add values to your object so 
 just imagine it's already an object, even if it will only 
 become one when you'll start to add some values.
You're wrong, I need an empty object for an API call! Moreover "{}" it's a valid json object and there's no easy way to create it using std.json.
It's true that it can be problematic, for example with an input contract, or for subtyping a JSONValue as something like struct JSONValueThatAlwayObject{}... But there is only 3 ways in std.json, from a literal, using the deprecated way or the "lazy" way.
Jan 22 2016
parent Chris Wright <dhasenan gmail.com> writes:
On Fri, 22 Jan 2016 17:34:58 +0000, userABCabc123 wrote:
 It's true that it can be problematic, for example with an input
 contract, or for subtyping a JSONValue as something like struct
 JSONValueThatAlwayObject{}...
 
 But there is only 3 ways in std.json, from a literal, using the
 deprecated way or the "lazy" way.
Which works out to one way that actually has the behavior that Andrea needs, and that involves parsing javascript. That's not ideal. Vibe.d has a static method 'emptyObject' that returns a new empty object. That would be handy here.
Jan 22 2016
prev sibling parent Borislav Kosharov <boby_dsm abv.bg> writes:
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
Jan 22 2016