Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
January 22, 2016 Create an empty json object with std.json | ||||
---|---|---|---|---|
| ||||
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; |
January 22, 2016 Re: Create an empty json object with std.json | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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); } ---- |
January 22, 2016 Re: Create an empty json object with std.json | ||||
---|---|---|---|---|
| ||||
Posted in reply to userABCabc123 | 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!
|
January 22, 2016 Re: Create an empty json object with std.json | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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.
|
January 22, 2016 Re: Create an empty json object with std.json | ||||
---|---|---|---|---|
| ||||
Posted in reply to userABCabc123 | 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.
|
January 22, 2016 Re: Create an empty json object with std.json | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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.
|
January 22, 2016 Re: Create an empty json object with std.json | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | 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 |
January 23, 2016 Re: Create an empty json object with std.json | ||||
---|---|---|---|---|
| ||||
Posted in reply to userABCabc123 | 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.
|
Copyright © 1999-2021 by the D Language Foundation