www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - unserialize variants

reply "gedaiu" <szabobogdan yahoo.com> writes:
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... There is 
a way of converting the resulted string back to an array of 
varianta?

thanks,
Bogdan
Aug 31 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Sep 01 2013
parent "gedaiu" <szabobogdan yahoo.com> writes:
Thanks for the response... I thought there is a faster way for 
that. I will use the standard lib or i will use json to store 
that into a file.


Thanks,
Bogdan




On Sunday, 1 September 2013 at 16:19:32 UTC, Ali Çehreli wrote:
 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
Sep 02 2013