On Sunday, 3 August 2014 at 07:16:05 UTC, Andrei Alexandrescu wrote:
We need a better json library at Facebook. I'd discussed with Sönke the possibility of taking vibe.d's json to std but he said it needs some more work. So I took std.jgrandson to proof of concept state and hence ready for destruction:

http://erdani.com/d/jgrandson.d
http://erdani.com/d/phobos-prerelease/std_jgrandson.html

Here are a few differences compared to vibe.d's library. I think these are desirable to have in that library as well:

* Parsing strings is decoupled into tokenization (which is lazy and only needs an input range) and parsing proper. Tokenization is lazy, which allows users to create their own advanced (e.g. partial/lazy) parsing if needed. The parser itself is eager.

* There's no decoding of strings.

* The representation is built on Algebraic, with the advantages that it benefits from all of its primitives. Implementation is also very compact because Algebraic obviates a bunch of boilerplate. Subsequent improvements to Algebraic will also reflect themselves into improvements to std.jgrandson.

* The JSON value (called std.jgrandson.Value) has no named member variables or methods except for __payload. This is so there's no clash between dynamic properties exposed via opDispatch.

Well that's about it. What would it take for this to become a Phobos proposal? Destroy.


Andrei

On my bson library I found very useful to have some methods to know if a field exists or not, and to get a "defaulted" value. Something like:

auto assume(T)(Value v, T default = T.init);

Another good method could be something like xpath to get a deep value:

Value v = value["/path/to/sub/object"];

Moreover in my library I actually have three different methods to read a value:

T get(T)() // Exception if value is not a T or not valid or value doesn't exist T to(T)() // Try to convert value to T using to!string. Exception if doesn't exists or not valid

BsonField!T as(T)(lazy T default = T.init) // Always return a value

BsonField!T is an "alias this"-ed struct with two fields: T value and bool error(). T value is the aliased field, and error() tells you if value is defaulted (because of an error: field not exists or can't convert to T)

So I can write something like this:

int myvalue = json["/that/deep/property"].as!int;

or

auto myvalue = json["/that/deep/property"].as!int(10);

if (myvalue.error) writeln("Property doesn't exists, I'm using default value);

writeln("Property value: ", myvalue);

I hope this can be useful...



Reply via email to