Wanted: Review manager for std.data.json

via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 28 02:30:13 PDT 2015


On Wednesday, 8 April 2015 at 10:02:30 UTC, Sönke Ludwig wrote:
> Am 08.04.2015 um 10:24 schrieb Andrea Fontana:
>> Any plan to support functions like these?
>>
>> http://forum.dlang.org/thread/lrknjl$co7$1@digitalmars.com?page=4#post-bcszdbasnjzmbwzdgeqy:40forum.dlang.org
>
> There is opt() [1], which takes a path and returns a 
> `Nullable!JSONValue`. "get" is unfortunately already taken by 
> std.typecons.Algebraic, so we can't use it for its AA meaning.
>
> Maybe another overload of opt?
> JSONValue opt(JSONValue default_value, string[] path...);
>
> [1]: 
> http://s-ludwig.github.io/std_data_json/stdx/data/json/value/json_value.opt.html

This seems a bit awkward to use. For example, it cannot handle 
numeric indices. Could we instead have an `undefined` value, that 
evaluates to `false` in a boolean context, and otherwise behaves 
as if it were absent? The two `opIndex` overloads will again 
return `undefined` for this value, as well as for non-objects and 
non-arrays, and for non-existing elements. Then you could access 
JSON values in an intuitive way without worrying about getting 
Exceptions or even Errors:

     writeln(json[42]["abc"][10 .. 20].get!int);

Only the `get` operation would throw if the type is wrong.

This also allows for easy and unambiguous testing whether an 
object contains a key:

     if(json["bla"] == JSONValue.undefined) { writeln("not 
found"); }

Anyway, it needs to be made clear what the behaviour on 
out-of-bounds or non-existing keys is: `undefined` value, 
exception, error?


More information about the Digitalmars-d mailing list