digitalmars.D.learn - Serializer class
- houdoux09 (31/31) Feb 22 2017 Hello, i am new in D and i have a problem.
- thedeemon (4/16) Feb 22 2017 You need to use "static if" here inside foreach. Otherwise the
- houdoux09 (1/1) Feb 22 2017 Yes thank you it works.
- Jacob Carlborg (5/21) Feb 24 2017 I'm pretty sure you need to use "value.tupleof[i][0]" instead of "mm[0]"...
- houdoux09 (40/42) Feb 24 2017 it does not work but I found a solution, that's what I do :
- Jacob Carlborg (6/7) Feb 25 2017 Cast the value to the type of the base class and run it through the same...
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
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
On 2017-02-22 20:13, thedeemon wrote:On Wednesday, 22 February 2017 at 18:34:26 UTC, houdoux09 wrote:I'm pretty sure you need to use "value.tupleof[i][0]" instead of "mm[0]" as well. -- /Jacob Carlborgvoid 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 24 2017
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
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