Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 31, 2014 How to set JSON_TYPE in 2.0.65 | ||||
---|---|---|---|---|
| ||||
Hi guys, I updated my dmd version from 2.0.63 to 2.0.65 and having some trouble with my JSON-definitions. The code below worked in 2.0.63 but not in 2.0.65: JSONValue oJson = JSONValue(); oJson.type = JSON_TYPE.OBJECT; // FAILS oJson.object["myEntry"] = JSONValue(); oJson.object["myEntry"].type = JSON_TYPE.STRING; // FAILS oJSon.object["myEntry"].str = "MyText"; Since the type property is a constant in 2.0.65, I've no idea how I can set the type for my JSON entries. I already tried the following: JSONValue oJson = JSONValue(JSON_TYPE.OBJECT); oJson.object["myEntry"] = JSONValue(JSON_TYPE.STRING); oJSon.object["myEntry"].str = "MyText"; But this gives me something like: "JSONValue is not an object". Any idea how I can set the corresponding value type? |
March 31, 2014 Re: How to set JSON_TYPE in 2.0.65 | ||||
---|---|---|---|---|
| ||||
Posted in reply to nrgyzer | On 03/31/2014 02:59 PM, nrgyzer wrote: > Hi guys, > > I updated my dmd version from 2.0.63 to 2.0.65 and having some trouble with my JSON-definitions. The code below worked in 2.0.63 but not in 2.0.65: > > JSONValue oJson = JSONValue(); > oJson.type = JSON_TYPE.OBJECT; // FAILS > oJson.object["myEntry"] = JSONValue(); > oJson.object["myEntry"].type = JSON_TYPE.STRING; // FAILS > oJSon.object["myEntry"].str = "MyText"; > > Since the type property is a constant in 2.0.65, I've no idea how I can set the type for my JSON entries. I already tried the following: > > JSONValue oJson = JSONValue(JSON_TYPE.OBJECT); > oJson.object["myEntry"] = JSONValue(JSON_TYPE.STRING); > oJSon.object["myEntry"].str = "MyText"; > > But this gives me something like: "JSONValue is not an object". > > Any idea how I can set the corresponding value type? I believe that this was simplified in recent releases, so one only needs to do the following: JSONValue oJson; oJson.object["myEntry"] = "MyText"; (Untested but I believe this is how it's supposed to be now) -- Matt Soucy http://msoucy.me/ |
March 31, 2014 Re: How to set JSON_TYPE in 2.0.65 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt Soucy | On Mon, 31 Mar 2014 15:05:10 -0400, Matt Soucy wrote:
> I believe that this was simplified in recent releases, so one only needs to do the following:
>
> JSONValue oJson;
> oJson.object["myEntry"] = "MyText";
>
> (Untested but I believe this is how it's supposed to be now)
That's correct, the type field has been moved to a read-only property. JSONValue should derive the appropriate type via assignment:
JSONValue i = 2; // type == INTEGER
JSONValue s = "s"; // type == STRING
JSONValue a = [ JSONValue(1.0) ]; // type == ARRAY (of FLOAT)
Note that you can also use the constructor (calls opAssign internally).
|
April 01, 2014 Re: How to set JSON_TYPE in 2.0.65 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt Soucy | On Monday, 31 March 2014 at 19:05:11 UTC, Matt Soucy wrote:
> On 03/31/2014 02:59 PM, nrgyzer wrote:
>> Hi guys,
>>
>> I updated my dmd version from 2.0.63 to 2.0.65 and having some trouble
>> with my JSON-definitions. The code below worked in 2.0.63 but not in
>> 2.0.65:
>>
>> JSONValue oJson = JSONValue();
>> oJson.type = JSON_TYPE.OBJECT; // FAILS
>> oJson.object["myEntry"] = JSONValue();
>> oJson.object["myEntry"].type = JSON_TYPE.STRING; // FAILS
>> oJSon.object["myEntry"].str = "MyText";
>>
>> Since the type property is a constant in 2.0.65, I've no idea how I can
>> set the type for my JSON entries. I already tried the following:
>>
>> JSONValue oJson = JSONValue(JSON_TYPE.OBJECT);
>> oJson.object["myEntry"] = JSONValue(JSON_TYPE.STRING);
>> oJSon.object["myEntry"].str = "MyText";
>>
>> But this gives me something like: "JSONValue is not an object".
>>
>> Any idea how I can set the corresponding value type?
> I believe that this was simplified in recent releases, so one only needs
> to do the following:
>
> JSONValue oJson;
> oJson.object["myEntry"] = "MyText";
>
> (Untested but I believe this is how it's supposed to be now)
I believe for this case you would want this:
JSONValue oJson = [ "myEntry": "MyText" ];
|
April 01, 2014 Re: How to set JSON_TYPE in 2.0.65 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Colden Cullen | >
> I believe for this case you would want this:
> JSONValue oJson = [ "myEntry": "MyText" ];
Yeah, and unfortunately, there is no that easy way to create an empty "object" type json value though. (Yes, I had a need for that). I needed to work around of that.
|
April 03, 2014 Re: How to set JSON_TYPE in 2.0.65 | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matt Soucy | On Monday, 31 March 2014 at 19:05:11 UTC, Matt Soucy wrote:
> On 03/31/2014 02:59 PM, nrgyzer wrote:
>> Hi guys,
>>
>> I updated my dmd version from 2.0.63 to 2.0.65 and having some trouble
>> with my JSON-definitions. The code below worked in 2.0.63 but not in
>> 2.0.65:
>>
>> JSONValue oJson = JSONValue();
>> oJson.type = JSON_TYPE.OBJECT; // FAILS
>> oJson.object["myEntry"] = JSONValue();
>> oJson.object["myEntry"].type = JSON_TYPE.STRING; // FAILS
>> oJSon.object["myEntry"].str = "MyText";
>>
>> Since the type property is a constant in 2.0.65, I've no idea how I can
>> set the type for my JSON entries. I already tried the following:
>>
>> JSONValue oJson = JSONValue(JSON_TYPE.OBJECT);
>> oJson.object["myEntry"] = JSONValue(JSON_TYPE.STRING);
>> oJSon.object["myEntry"].str = "MyText";
>>
>> But this gives me something like: "JSONValue is not an object".
>>
>> Any idea how I can set the corresponding value type?
> I believe that this was simplified in recent releases, so one only needs
> to do the following:
>
> JSONValue oJson;
> oJson.object["myEntry"] = "MyText";
>
> (Untested but I believe this is how it's supposed to be now)
Thanks Matt, but this doesn't work because the object-property checks the type of the JSONValue and throws the corresponding exception. But the following works:
JSONValue oJson = ["myEntry" : "MyText"];
oJson.object["myEntry_2"] = "MyText_2";
...
Thanks to all!
|
Copyright © 1999-2021 by the D Language Foundation