RFC: std.json sucessor

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


On 8/22/14, 3:33 AM, Sönke Ludwig wrote:
> Am 22.08.2014 02:42, schrieb Ary Borenszweig:
>> Say I have a class Person with name (string) and age (int) with a
>> constructor that receives both. How would I create an instance of a
>> Person from a json with the json stream?
>>
>> Suppose the json is this:
>>
>> {"age": 10, "name": "John"}
>>
>> And the class is this:
>>
>> class Person {
>>    this(string name, int age) {
>>      // ...
>>    }
>> }
>>
>
> 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?


More information about the Digitalmars-d mailing list