unserialize variants

Ali Çehreli acehreli at yahoo.com
Sun Sep 1 09:19:31 PDT 2013


On 08/31/2013 10:22 PM, gedaiu wrote:> Hi,
 >
 > i want to save data from an array of variants into a file. I saw that
 > to!string format the array content in a nice way...

I don't think the format is sufficient for recreating the array:

import std.variant;
import std.conv;
import std.stdio;

struct S
{
     int i;
}

void main()
{
     auto a = [ Variant(42), Variant("hello"), Variant(S(5)) ];
     writeln(a);
}

Outputs:

[42, hello, S(5)]

We can only guess that 'hello' is a string but what if it were the 
string "43"? It would look like an int. Also, any type can overload 
toString; so, S(5) could output itself as e.g. "world".

 > There is a way of
 > converting the resulted string back to an array of varianta?

This requires a serialization module. std.serialization is in review 
right now:

   http://forum.dlang.org/post/hsnmxykmoytfvwroikzk@forum.dlang.org

Its author Jacob Carlborg already has a serialization module called Orange:

   https://github.com/jacob-carlborg/orange

 >
 > thanks,
 > Bogdan

Ali



More information about the Digitalmars-d-learn mailing list