digitalmars.D.learn - Create an empty json object with std.json
- Andrea Fontana (14/14) Jan 22 2016 If you declare a JSONValue like this:
- userABCabc123 (14/28) Jan 22 2016 read this:
- Andrea Fontana (2/13) Jan 22 2016 That's right, but I need an empty object, without any key set!
- userABCabc123 (6/21) Jan 22 2016 But soon or later you'll need to add values to your object so
- Andrea Fontana (4/7) Jan 22 2016 You're wrong, I need an empty object for an API call!
- userABCabc123 (6/13) Jan 22 2016 It's true that it can be problematic, for example with an input
- Chris Wright (5/11) Jan 22 2016 Which works out to one way that actually has the behavior that Andrea
- Borislav Kosharov (7/21) Jan 22 2016 What you can do is use the JSONValue ctor and pass it an empty
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
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
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
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: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.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
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
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: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.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
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
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