www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Best way to save/load an object to file?

reply "Saaa" <empty needmail.com> writes:
As the subject says  : )

I could make my own personal loading saving functions of all the specific 
variables in the objects, but I'd love there to be an easier way than that.
Jun 18 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Saaa,

 As the subject says  : )
 
 I could make my own personal loading saving functions of all the
 specific variables in the objects, but I'd love there to be an easier
 way than that.
 

It might be possible to generate a generic save device using tuple of and some magic to detect and deal with references (convert them to file references with some caching and delayed output to deal with loops and whatnot) <joke> I could show you how to do this, but then the knowledge would kill you </joke>
Jun 18 2008
parent "Saaa" <empty needmail.com> writes:
Lol, I think I'm not really that good a magician :)

A box model would suffice I think:
box class 1{
auto colors[]=fruitsClass.colors[];
..
..
}

save box to file
--
load box from file would set all values.

Are there boxes like this?

 It might be possible to generate a generic save device using tuple of and 
 some magic to detect and deal with references (convert them to file 
 references with some caching and delayed output to deal with loops and 
 whatnot)

 <joke> I could show you how to do this, but then the knowledge would kill 
 you </joke>
 

Jun 20 2008
prev sibling next sibling parent reply Lutger <lutger.blijdestin gmail.com> writes:
Saaa wrote:

 As the subject says  : )
 
 I could make my own personal loading saving functions of all the specific
 variables in the objects, but I'd love there to be an easier way than
 that.

If you use Tango, you'll probably want to look into protocols. Haven't used it myself yet, so I can't comment on it. There might be other serialization code around somewhere over at dsource though, arclib used to have some but I believe that makes use of Tango's now.
Jun 19 2008
parent "Saaa" <empty needmail.com> writes:
I could use tango.. wouldn't be that much of a difference. I need to take a 
deeper look into these things (I've just seen the arclib serializer :).
But as mentioned above, a simpler approach would suffice.

 If you use Tango, you'll probably want to look into protocols. Haven't 
 used
 it myself yet, so I can't comment on it. There might be other 
 serialization
 code around somewhere over at dsource though, arclib used to have some but
 I believe that makes use of Tango's now. 

Jun 20 2008
prev sibling next sibling parent reply aarti_pl <aarti interia.pl> writes:
Saaa pisze:
 As the subject says  : )
 
 I could make my own personal loading saving functions of all the specific 
 variables in the objects, but I'd love there to be an easier way than that.

You may try doost serializer: http://dsource.org/projects/doost/ You can serialize to file (generally: different kinds of storages) or to string. There are 2 kinds of archives: simple text archive and JSON archive. See below for use cases: http://dsource.org/projects/doost/browser/trunk/examples/util/serializer/FunctionTest.d http://dsource.org/projects/doost/browser/trunk/examples/util/serializer/FunctionTest1.d Simplest use case for storing in file you can find in FunctionTest1.d - 3rd unit test from top. Currently Phobos only, but should be rather easy to adapt for Tango. BR Marcin Kuszczak (aarti_pl)
Jun 22 2008
next sibling parent reply "Saaa" <empty needmail.com> writes:
Looks interesting, thanks.
I need to check the code a bit more, but the creating-JSON-archive-part 
really strikes my fancy :)
Phobos only wouldn't be a problem for me.
And as I read it, the boost license lets me use the code in compiled form 
without having to put the license in text form. Another plus!

 You may try doost serializer:
 http://dsource.org/projects/doost/

 You can serialize to file (generally: different kinds of storages) or to 
 string. There are 2 kinds of archives: simple text archive and JSON 
 archive.

 See below for use cases:
 http://dsource.org/projects/doost/browser/trunk/examples/util/serializer/FunctionTest.d
 http://dsource.org/projects/doost/browser/trunk/examples/util/serializer/FunctionTest1.d

 Simplest use case for storing in file you can find in FunctionTest1.d - 
 3rd unit test from top.

 Currently Phobos only, but should be rather easy to adapt for Tango.

 BR
 Marcin Kuszczak
 (aarti_pl)
 

Jun 22 2008
parent aarti_pl <aarti interia.pl> writes:
Saaa pisze:
 Looks interesting, thanks.
 I need to check the code a bit more, but the creating-JSON-archive-part 
 really strikes my fancy :)
 Phobos only wouldn't be a problem for me.
 And as I read it, the boost license lets me use the code in compiled form 
 without having to put the license in text form. Another plus!
 
 You may try doost serializer:
 http://dsource.org/projects/doost/

 You can serialize to file (generally: different kinds of storages) or to 
 string. There are 2 kinds of archives: simple text archive and JSON 
 archive.

 See below for use cases:
 http://dsource.org/projects/doost/browser/trunk/examples/util/serializer/FunctionTest.d
 http://dsource.org/projects/doost/browser/trunk/examples/util/serializer/FunctionTest1.d

 Simplest use case for storing in file you can find in FunctionTest1.d - 
 3rd unit test from top.

 Currently Phobos only, but should be rather easy to adapt for Tango.

 BR
 Marcin Kuszczak
 (aarti_pl)


Do not hesitate to put comments on forum regarding API/problems/suggestions. Hope that this library will be useful for you. BR Marcin Kuszczak aarti_pl
Jun 23 2008
prev sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
aarti_pl wrote:
 Saaa pisze:
 As the subject says  : )

 I could make my own personal loading saving functions of all the 
 specific variables in the objects, but I'd love there to be an easier 
 way than that.

You may try doost serializer: http://dsource.org/projects/doost/ You can serialize to file (generally: different kinds of storages) or to string. There are 2 kinds of archives: simple text archive and JSON archive. See below for use cases: http://dsource.org/projects/doost/browser/trunk/examples/util/seria izer/FunctionTest.d http://dsource.org/projects/doost/browser/trunk/examples/util/serial zer/FunctionTest1.d Simplest use case for storing in file you can find in FunctionTest1.d - 3rd unit test from top. Currently Phobos only, but should be rather easy to adapt for Tango. BR Marcin Kuszczak (aarti_pl)

Sweet. Thanks for reminding me about that! I'm in the market for some serialization code too. Does it handle references properly? Meaning if I have class objects A and B that both refer to the same C, will C only get serialized once? --bb
Jun 22 2008
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Bill Baxter wrote:
 aarti_pl wrote:
 Saaa pisze:
 As the subject says  : )

 I could make my own personal loading saving functions of all the 
 specific variables in the objects, but I'd love there to be an easier 
 way than that.

You may try doost serializer: http://dsource.org/projects/doost/ You can serialize to file (generally: different kinds of storages) or to string. There are 2 kinds of archives: simple text archive and JSON archive. See below for use cases: http://dsource.org/projects/doost/browser/trunk/examples/util/seria izer/FunctionTest.d http://dsource.org/projects/doost/browser/trunk/examples/util/serial zer/FunctionTest1.d Simplest use case for storing in file you can find in FunctionTest1.d - 3rd unit test from top. Currently Phobos only, but should be rather easy to adapt for Tango. BR Marcin Kuszczak (aarti_pl)

Sweet. Thanks for reminding me about that! I'm in the market for some serialization code too. Does it handle references properly? Meaning if I have class objects A and B that both refer to the same C, will C only get serialized once?

Just wanted to note here, that after playing with it a bit it does seem to handle references properly. It also manages to serialize simple classes without any intrusive instrumentation whatsoever. Very nice! As usual with doost, though, it could use a little documentation. And more comments in the code. The few comments there are in Polish, which isn't much use to me. I couldn't find the serializer in arclib. What file is it in? --bb
Jun 23 2008
next sibling parent reply Christian Kamm <kamm-incasoftware shift-at-right.de> writes:
Bill Baxter Wrote:
 I couldn't find the serializer in arclib.  What file is it in?

Maybe Clay or I removed it. It can always be found here: http://www.dsource.org/projects/serialization It is discontinued though. Christian
Jun 23 2008
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Christian Kamm wrote:
 Bill Baxter Wrote:
 I couldn't find the serializer in arclib.  What file is it in?

Maybe Clay or I removed it. It can always be found here: http://www.dsource.org/projects/serialization It is discontinued though. Christian

I see. From a quick look at the test file, I think Aarti's code is a bit further along in development than that was. --bb
Jun 23 2008
prev sibling parent aarti_pl <aarti interia.pl> writes:
Bill Baxter pisze:
 As usual with doost, though, it 
 could use a little documentation.  And more comments in the code.  The 
 few comments there are in Polish, which isn't much use to me.
 
 --bb

Yes documentation is unfortunately not strongest point of Doost :-( I will try to improve this situation in future, but it takes a lot of time, especially for me as I am not native English speaker. And sadly time is not from elastic rubber :-] Another think which stopped a bit development was uncertainty about current API design (especially everything connected with 'storage' concept). Current design works well, but I would be happy to get some feedback from real users. BR Marcin Kuszczak (aarti_pl)
Jun 23 2008
prev sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Saaa wrote:
 As the subject says  : )
 
 I could make my own personal loading saving functions of all the specific 
 variables in the objects, but I'd love there to be an easier way than that.
 
 

In addition to doost.util.serializer, which has been mentioned, h3r3tic has pointed out to me that team0xf has a library called xpose which does serialization to and from a binary format. Can be found here: http://team0xf.com:8080/xf/file/34c9c0ffde3a/xpose/Serialization.d Entire source included in xf, which you can get using hg: hg clone http://team0xf.com:8080/xf --bb
Jun 26 2008