serialization library

Christian Kamm kamm at nospam.de
Wed Nov 8 13:43:00 PST 2006


Based on initial work from Tom S and clayasaurus, I've written this  
serialization library. If hope something like this doesn't already exist!

http://www.math.tu-berlin.de/~kamm/d/serialization.zip

Currently, it only provides binary file io through the Serializer class.  
It can
- write/read almost (hopefully) every type through a call to  
Serializer.describe
- track class references and pointers by default
- serialize classes and structs through a templated 'describe' member  
function
- write derived classes from base class reference*
- read derived classes into base class reference*
- serialize not default constructible classes*

(* for this to work, the class needs to be registered with the archive  
type)

It has far less features than boost::serialization but is already in a  
very usable state: FreeUniverse, a D game based on the Arc library, uses  
it for writing and loading savegames as well as other persistant state  
information.

What it does not do/is missing:
- exception safety / multithread safety
- out-of-class/struct serialization methods (is it possible to check  
whether a specific overload exists at compile time?)
- static arrays need to be serialized with describe_staticarray (static  
arrays can't be inout, so the general-purpose template method doesn't  
work... is there a way around the problem?)
- things I forgot right now

Documentation is still rather sparse. This short example shows the basic  
usage

---
struct Foo
{
   int a = 3;

   void describe(T)(T archive)
   {
     archive.describe(a);
   }
}

void main()
{
   real bar = 3.141;
   Foo foo;

   // write data
   Serializer s = new Serializer("testfile", FileMode.Out);
   s.describe(bar);
   s.describe(foo);
   delete s;

   // read data
   s = new Serializer("testfile", FileMode.In);
   s.describe(bar);
   s.describe(foo);
}
---

See the unittests in serializer.d for other details. Most of the logic is  
in basicarchive.d. Docs definitely need work.

Since FreeUniverse was its first real user, it is currently maintained in  
the FreeUniverse svn. However, if other people are interested, I will  
request a seperate project for it on dsource.

Comments and improvements are of course welcome.

Best Regards,
Christian Kamm



More information about the Digitalmars-d-announce mailing list