D JSON (WAT?!)

Pavel via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 24 08:33:07 PDT 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???


More information about the Digitalmars-d-learn mailing list