Jump to page: 1 24  
Page
Thread overview
D JSON (WAT?!)
Jul 24, 2014
Pavel
Jul 24, 2014
Justin Whear
Jul 24, 2014
Pavel
Jul 24, 2014
Adam D. Ruppe
Jul 24, 2014
bearophile
Jul 24, 2014
Pavel
Jul 24, 2014
Daniel Gibson
Jul 24, 2014
Pavel
Jul 24, 2014
Ali Çehreli
Jul 24, 2014
Pavel
Jul 24, 2014
John Colvin
Jul 24, 2014
John Colvin
Jul 24, 2014
Pavel
Jul 24, 2014
Pavel
Jul 24, 2014
Edwin van Leeuwen
Jul 24, 2014
Pavel
Jul 24, 2014
bearophile
Jul 24, 2014
Daniel Gibson
Jul 24, 2014
Pavel
Jul 24, 2014
Justin Whear
Jul 24, 2014
Pavel
Jul 24, 2014
Justin Whear
Jul 24, 2014
Ary Borenszweig
Jul 24, 2014
Justin Whear
Jul 24, 2014
Ary Borenszweig
Jul 24, 2014
Pavel
Jul 24, 2014
Pavel
Jul 25, 2014
Justin Whear
Jul 26, 2014
Ary Borenszweig
Jul 26, 2014
Meta
Jul 26, 2014
Kagamin
Aug 08, 2014
Pavel
Aug 08, 2014
Justin Whear
Jul 24, 2014
Justin Whear
Jul 24, 2014
H. S. Teoh
Jul 24, 2014
Pavel
Jul 24, 2014
John Colvin
Jul 24, 2014
Pavel
July 24, 2014
Ok, let me start with the sample code:

import std.stdio;
import std.json;

void main() {
  scope(failure) writeln("FaILED!!");
  string jsonStr = `{ "name": "1", "type": "r" }`;
  auto parsed = parseJSON(jsonStr);
  string s = parsed["fail"].str;
  writeln(s == "");
  writeln(s is null);
  writeln(s);
}

Running "rdmd app.d" doesn't produce any output.
Can anyone explain such a behavior???


PS: Running dmd v2.065 on Linux x64.
July 24, 2014
On Thu, 24 Jul 2014 15:15:36 +0000, Pavel wrote:

> Ok, let me start with the sample code:
> 
> import std.stdio;
> import std.json;
> 
> void main() {
>    scope(failure) writeln("FaILED!!");
>    string jsonStr = `{ "name": "1", "type": "r" }`;
>    auto parsed = parseJSON(jsonStr);
>    string s = parsed["fail"].str;
>    writeln(s == "");
>    writeln(s is null);
>    writeln(s);
> }
> 
> Running "rdmd app.d" doesn't produce any output.
> Can anyone explain such a behavior???
> 
> 
> PS: Running dmd v2.065 on Linux x64.

That's because it produces a segmentation fault, which rdmd masks for some reason.  The `parsed["fail"]` should throw a range bounds exception-- not sure why it's not.
July 24, 2014
On Thursday, 24 July 2014 at 15:15:37 UTC, Pavel wrote:
>   string s = parsed["fail"].str;

Since there is no entry "fail" in the object, it returns a null JSON_VALUE pointer. Trying to get the string out of it is then seen as a null pointer access and kills the program.

Check for null on a key before trying to get a value out.
July 24, 2014
Adam D. Ruppe:

> Check for null on a key before trying to get a value out.

Is something like the get() function usable here?

Bye,
bearophile
July 24, 2014
On Thursday, 24 July 2014 at 15:22:46 UTC, Adam D. Ruppe wrote:
> On Thursday, 24 July 2014 at 15:15:37 UTC, Pavel wrote:
>>  string s = parsed["fail"].str;
>
> Since there is no entry "fail" in the object, it returns a null JSON_VALUE pointer. Trying to get the string out of it is then seen as a null pointer access and kills the program.
>
> Check for null on a key before trying to get a value out.

Ok, added:

writeln(parsed["fail"] == null);

Now compiler complains:

Error: incompatible types for ((parsed.opIndex("fail")) == (null)): 'JSONValue' and 'typeof(null)'


WAT?!
July 24, 2014
Am 24.07.2014 17:29, schrieb Pavel:
> On Thursday, 24 July 2014 at 15:22:46 UTC, Adam D. Ruppe wrote:
>> On Thursday, 24 July 2014 at 15:15:37 UTC, Pavel wrote:
>>>  string s = parsed["fail"].str;
>>
>> Since there is no entry "fail" in the object, it returns a null
>> JSON_VALUE pointer. Trying to get the string out of it is then seen as
>> a null pointer access and kills the program.
>>
>> Check for null on a key before trying to get a value out.
>
> Ok, added:
>
> writeln(parsed["fail"] == null);
>
> Now compiler complains:
>
> Error: incompatible types for ((parsed.opIndex("fail")) == (null)):
> 'JSONValue' and 'typeof(null)'
>
>
> WAT?!


"is" instead of "==" ?
July 24, 2014
On Thursday, 24 July 2014 at 15:15:37 UTC, Pavel wrote:
> Ok, let me start with the sample code:
>
> import std.stdio;
> import std.json;
>
> void main() {
>   scope(failure) writeln("FaILED!!");
>   string jsonStr = `{ "name": "1", "type": "r" }`;
>   auto parsed = parseJSON(jsonStr);
>   string s = parsed["fail"].str;
>   writeln(s == "");
>   writeln(s is null);
>   writeln(s);
> }
>
> Running "rdmd app.d" doesn't produce any output.
> Can anyone explain such a behavior???
>
>
> PS: Running dmd v2.065 on Linux x64.

It's a bug in std.json (you should get a segfault, not no output at all)

It is fixed now and I'm pretty sure it will be in 2.066

std.json has been improved a lot, but I would still recommend using http://vibed.org/api/vibe.data.json/ instead
July 24, 2014
On Thursday, 24 July 2014 at 15:20:58 UTC, Justin Whear wrote:
> On Thu, 24 Jul 2014 15:15:36 +0000, Pavel wrote:
>
>> Ok, let me start with the sample code:
>> 
>> import std.stdio;
>> import std.json;
>> 
>> void main() {
>>    scope(failure) writeln("FaILED!!");
>>    string jsonStr = `{ "name": "1", "type": "r" }`;
>>    auto parsed = parseJSON(jsonStr);
>>    string s = parsed["fail"].str;
>>    writeln(s == "");
>>    writeln(s is null);
>>    writeln(s);
>> }
>> 
>> Running "rdmd app.d" doesn't produce any output.
>> Can anyone explain such a behavior???
>> 
>> 
>> PS: Running dmd v2.065 on Linux x64.
>
> That's because it produces a segmentation fault, which rdmd masks for
> some reason.  The `parsed["fail"]` should throw a range bounds exception--
> not sure why it's not.

Here's phobos code from github:

/// Hash syntax for json objects.
    /// Throws $(D JSONException) if $(D type) is not $(D JSON_TYPE.OBJECT).
    ref inout(JSONValue) opIndex(string k) inout
    {
        enforceEx!JSONException(type == JSON_TYPE.OBJECT,
                                "JSONValue is not an object");
        return *enforceEx!JSONException(k in store.object,
                                        "Key not found: " ~ k);
    }

Added in https://github.com/D-Programming-Language/phobos/commit/13fbd451bcca923cf8d1028495e58aa88cc7efeb

Maybe phobos is not up to date for me???
July 24, 2014
On 07/24/2014 08:29 AM, Pavel wrote:

> writeln(parsed["fail"] == null);
>
> Now compiler complains:
>
> Error: incompatible types for ((parsed.opIndex("fail")) == (null)):
> 'JSONValue' and 'typeof(null)'
>
>
> WAT?!

Comparing against null should be done with the 'is' operator, not the == operator:

    if (x is null)

Ali

July 24, 2014
On Thursday, 24 July 2014 at 15:31:30 UTC, Daniel Gibson wrote:
> Am 24.07.2014 17:29, schrieb Pavel:
>> On Thursday, 24 July 2014 at 15:22:46 UTC, Adam D. Ruppe wrote:
>>> On Thursday, 24 July 2014 at 15:15:37 UTC, Pavel wrote:
>>>> string s = parsed["fail"].str;
>>>
>>> Since there is no entry "fail" in the object, it returns a null
>>> JSON_VALUE pointer. Trying to get the string out of it is then seen as
>>> a null pointer access and kills the program.
>>>
>>> Check for null on a key before trying to get a value out.
>>
>> Ok, added:
>>
>> writeln(parsed["fail"] == null);
>>
>> Now compiler complains:
>>
>> Error: incompatible types for ((parsed.opIndex("fail")) == (null)):
>> 'JSONValue' and 'typeof(null)'
>>
>>
>> WAT?!
>
>
> "is" instead of "==" ?

Nope, the compiler still complains.
« First   ‹ Prev
1 2 3 4