RFC: std.json sucessor

Ary Borenszweig via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 22 10:08:00 PDT 2014


On 8/22/14, 1:24 PM, Sönke Ludwig wrote:
> Am 22.08.2014 16:53, schrieb Ary Borenszweig:
>> On 8/22/14, 3:33 AM, Sönke Ludwig wrote:
>>> Without a serialization framework it would in theory work like this:
>>>
>>>      JSONValue v = parseJSON(`{"age": 10, "name": "John"}`);
>>>      auto p = new Person(v["name"].get!string, v["age"].get!int);
>>>
>>> unfortunately the operator overloading doesn't work like this currently,
>>> so this is needed:
>>>
>>>      JSONValue v = parseJSON(`{"age": 10, "name": "John"}`);
>>>      auto p = new Person(
>>>          v.get!(Json[string])["name"].get!string,
>>>          v.get!(Json[string])["age"].get!int);
>>
>> But does this parse the whole json into JSONValue? I want to create a
>> Person without creating an intermediate JSONValue for the whole json.
>> Can this be done?
>
> That would be done by the serialization framework. Instead of using
> parseJSON(), it could use parseJSONStream() to populate the Person
> instance on the fly, without putting the whole JSON into memory. But I'd
> like to leave that for a later addition, because we'd otherwise end up
> with duplicate functionality once std.serialization gets finalized.
>
> Manually it would work similar to this:
>
> auto nodes = parseJSONStream(`{"age": 10, "name": "John"}`);
> with (JSONParserNode.Kind) {
>      enforce(nodes.front == objectStart);
>      nodes.popFront();
>      while (nodes.front != objectEnd) {
>          auto key = nodes.front.key;
>          nodes.popFront();
>          if (key == "name")
>              person.name = nodes.front.literal.string;
>          else if (key == "age")
>              person.age = nodes.front.literal.number;
>      }
> }

Cool, that looks good :-)


More information about the Digitalmars-d mailing list