digitalmars.D.learn - unserialize variants
- gedaiu (7/7) Aug 31 2013 Hi,
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (25/31) Sep 01 2013 I don't think the format is sufficient for recreating the array:
- gedaiu (6/42) Sep 02 2013 Thanks for the response... I thought there is a faster way for
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
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/orangethanks, BogdanAli
Sep 01 2013
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. Isaw thatto!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/orangethanks, BogdanAli
Sep 02 2013