Range interface for std.serialization

Jacob Carlborg doob at me.com
Thu Aug 22 00:08:27 PDT 2013


On 2013-08-21 22:21, Dicebot wrote:

> It should be range of strings - one call to popFront should serialize
> one object from input object range and provide matching string buffer.

How should nesting been handled for a format like XML? Example:

class Bar
{
     int a;
}

class Foo
{
     int b;
     Bar bar;
}

Currently the following XML is procured when serializing Foo:

<object runtimeType="main.Foo" type="main.Foo" key="0" id="0">
     <int key="b" id="1">4</int>
     <object runtimeType="main.Bar" type="main.Bar" key="bar" id="2">
         <int key="a" id="3">3</int>
     </object>
</object>

If I shouldn't return the whole object, Foo, how can we know that when 
the string for Bar is returned it should actually be nested inside Foo?

> I can't imagine a use case for this. Adding ranges just because you can
> is not very good :)

Ok.

> What this snippet should do?

That was just a dummy snippet to set the data. This is a slightly better 
example:

auto archive = new XmlArchive!(string);
auto serializer = new Serializer(archive);
serializer.serialize(new Object);

writeToFile("foo.xml", archive.data);

Now I want to deserialize the data back:

archive.data = readFromFile("foo.xml"); // Error, cannot covert 
ReadFromFileRange to string

> Range-based algorithms don't assign ranges. Transferring data from one
> range to another is done via copy(sourceRange, destRange) and similar
> tools.

So how should the API look like for setting the data used when 
deserializing, like this?

auto data = readFromFile("foo.xml");
auto archive = new XmlArchive!(string);
copy(data, archive.data);

> It looks like difficulties come from your initial assumption that one
> call to serialize/deserialize implies one object - in that model ranges
> hardly are useful. I don't think it is a reasonable restriction. What is
> practically useful is (de)serialization of large list of objects lazily
> - and this is a natural job for ranges.

It depends on how you look at it. Currently it's only possible to 
serialize a single object with a single call to "serialize". So if you 
want to serialize multiple objects you do as you would do normally in 
your code, use an array, a linked list or similar. An array is still a 
single object, though it contains multiple objects, that is handled 
perfectly fine.

The question is if a range should be treated as multiple objects, and 
not a single object (which it really is). How should it be serialized?

* Something like an array, resulting in this XML:

<array type="int" length="5" key="0" id="0">
     <int key="0" id="1">1</int>
     <int key="1" id="2">2</int>
     <int key="2" id="3">3</int>
     <int key="3" id="4">4</int>
     <int key="4" id="5">5</int>
</array>

* Or like calling "serialize" multiple times, resulting in this XML:

<int key="0" id="0">1</int>
<int key="1" id="1">2</int>
<int key="2" id="2">3</int>
<int key="3" id="3">4</int>
<int key="4" id="4">5</int>

* Or as a single object:

Then it would actually serialize the struct/class representing the range.

And the most important question, how should ranges be deserialized? One 
have to tell the serializer what type to return, otherwise it won't 
work. But the whole point of ranges is that you shouldn't need to know 
the type. Sometimes you cannot even name the type, i.e. Voldemort types.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list