www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Serializer class

reply houdoux09 <houdoux09 gmail.com> writes:
Hello, i am new in D and i have a problem.
I would like to retrieve the names of the variables and their 
value.
But i can not retrieve the value of the array.

class Test
{
     public int a;
     public Test[] b;

     this()
     {
       a = 1;
       b = [new Test(), new Test()];
     }
}

//======================================================================

void Read(T)(T value)
{
    foreach(i, mm; value.tupleof)
    {
       writeln(__traits(identifier, value.tupleof[i]), " = ", mm);

       if(isArray!(typeof(mm)))
       {
           Read(mm[0]); //Error
       }
    }
}

void main(string[] args)
{
    Read(new Test());
}

Thank you for your help.
Feb 22 2017
parent reply thedeemon <dlang thedeemon.com> writes:
On Wednesday, 22 February 2017 at 18:34:26 UTC, houdoux09 wrote:
 void Read(T)(T value)
 {
    foreach(i, mm; value.tupleof)
    {
       writeln(__traits(identifier, value.tupleof[i]), " = ", 
 mm);

       if(isArray!(typeof(mm)))
       {
           Read(mm[0]); //Error
       }
    }
 }
You need to use "static if" here inside foreach. Otherwise the body of your "if" gets compiled for all the different fields of "value" and of course fails for the fields that are not arrays.
Feb 22 2017
next sibling parent houdoux09 <houdoux09 gmail.com> writes:
Yes thank you it works.
Feb 22 2017
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2017-02-22 20:13, thedeemon wrote:
 On Wednesday, 22 February 2017 at 18:34:26 UTC, houdoux09 wrote:
 void Read(T)(T value)
 {
    foreach(i, mm; value.tupleof)
    {
       writeln(__traits(identifier, value.tupleof[i]), " = ", mm);

       if(isArray!(typeof(mm)))
       {
           Read(mm[0]); //Error
       }
    }
 }
You need to use "static if" here inside foreach. Otherwise the body of your "if" gets compiled for all the different fields of "value" and of course fails for the fields that are not arrays.
I'm pretty sure you need to use "value.tupleof[i][0]" instead of "mm[0]" as well. -- /Jacob Carlborg
Feb 24 2017
parent reply houdoux09 <houdoux09 gmail.com> writes:
 I'm pretty sure you need to use "value.tupleof[i][0]" instead 
 of "mm[0]" as well.
it does not work but I found a solution, that's what I do : abstract class BaseClass { uint[] a = [9, 10, 5]; } override class Test : BaseClass { int t = 0; string s = "holla"; } public static JSONValue Serialize(BaseClass value) { foreach(member; __traits(allMembers, typeof(value))) { static if (__traits(compiles, to!string(__traits(getMember, value, member)))) { auto result = __traits(getMember, value, member); static if(isArray!(typeof(value)) && !is(typeof(value) == string)) { // process } else static if(is(typeof(value) == class) || is(typeof(value) == struct)) { // process } else { // process } } } void main(string[] args) { Serialize(new Test()); } The problem is that I can not retrieve the variables from the parent class.
Feb 24 2017
parent Jacob Carlborg <doob me.com> writes:
On 2017-02-24 19:10, houdoux09 wrote:

 The problem is that I can not retrieve the variables from the parent class.
Cast the value to the type of the base class and run it through the same function. You can have a look at the Orange serialization library [1]. [1] https://github.com/jacob-carlborg/orange -- /Jacob Carlborg
Feb 25 2017