Re: Serializer class

2017-02-25 Thread Jacob Carlborg via Digitalmars-d-learn

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


Re: Serializer class

2017-02-24 Thread houdoux09 via Digitalmars-d-learn
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.


Re: Serializer class

2017-02-24 Thread Jacob Carlborg via Digitalmars-d-learn

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


Re: Serializer class

2017-02-22 Thread houdoux09 via Digitalmars-d-learn

Yes thank you it works.



Re: Serializer class

2017-02-22 Thread thedeemon via Digitalmars-d-learn

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.




Serializer class

2017-02-22 Thread houdoux09 via Digitalmars-d-learn

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.