On 2016-09-20 21:45, Ram_B wrote:
I'm trying to set fields of object from JSON with traits library. How i
can to it properly?

import std.stdio;
import std.json;
import std.traits;
import std.meta:  Alias;

class Obj{
    void fromJSON(this T)(JSONValue j){
        foreach(field; FieldNameTuple!T){
            alias member = Alias!(__traits(getMember, T, field));
            static if (__traits(hasMember, member, "fromJSON")){
                member.fromJSON(j[field]);
            } else {
                member = j[field];

I'm pretty sure this won't work. You need to use "this.tupleof[index] = value" to set a value. You can iterate all fields using "T.tupleof" and then get the name of a field using "__traits(identifier, T.tupleof[index]);". Or you can use a string mixin.

--
/Jacob Carlborg

Reply via email to