RFC: std.json sucessor

Sönke Ludwig via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 21 23:33:26 PDT 2014


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);

That should be solved together with the new module (it could of course 
also easily be added to JSONValue itself instead of Algebraic, but the 
value of having it in Algebraic would be much higher).


More information about the Digitalmars-d mailing list