I’m still new to D, but an obvious missing feature (for web developers) in
http://www.digitalmars.com/d/2.0/phobos/std_json.html
is a mixin which creates JSON serialisers and deserialisers for arbitrary (nests) of structs and classes.
i.e.
struct Dog {
string name;
int age;
}
struct Person {
mixin JSON;
string name;
int age;
string[] favouriteFoods;
Dog dog;
bool retired () { return age > 65 };
}
then be able to
auto p = Person("\"name\":\"Fred\",\"age\":45,\"favouriteFoods\":[\"cheese\",\"bananas\"],\"dog\":{\"name\":\"Rover\",\"age\":7}");
p.dog.name -> "rover"
p.favouriteFoods[1] -> "bananas"
p.retired() -> false
and
p.toJSON(); -> "\"name\":\"Fred\",\"age\":45,\"favouriteFoods\":[\"cheese\",\"bananas\"],\"dog\":{\"name\":\"Rover\",\"age\":7}"
Would this be possible using the various meta programming features of D?
Thanks,
Chris.
Yes this is possible (I have a library which does exactly this in production right now), and D makes it pretty easy to implement using compile-time reflection. You’ll want to read up at these links: