jacob-carlborg/orange

Deserialize SysTime - error on immutable field

jmcabo opened this issue · 2 comments

Currently, one cannot deserialize SysTime variables. The compiler error produced is:

   orange\serialization\Serializer.di(1154): Error: variable orange.serialization.Serializer.Serializer.deserializeObject!   (immutable(TimeZone),string).deserializeObject.value cannot modify immutable

This is because the TimeZone of a SysTime is immutable.

Code to reproduce:

    class Foo
    {
            int a;
            SysTime s;
    }

    int main(string[] args) 
    {
    auto foo = new Foo; // create something to serialize
    foo.a = 3; // change the default value of "a"

    auto archive = new XmlArchive!(char); // create an XML archive
    auto serializer = new Serializer(archive); // create the serializer

    serializer.serialize(foo); // serialize "foo"

    println(archive.data); //debug

    // deserialize the serialized data as an instance of "Foo"
    auto f = serializer.deserialize!(Foo)(archive.untypedData);    //<- ERROR PRODUCED COMPILING THIS LINE.
    }

Please it would be very nice if you could add support for SysTime, since my app depends on it.

Thanks!

This looks tricky, I'll see what I can do.

I've finally been able to add support for (de)serializing const and immutable fields.

In this particular case you're serializing a SysTime which contains a TimeZone which is a singleton. To properly (de)serialize the TimeZone you first have to register LocalTime(which is a subclass of TimeZone). Then you need to also register a deserializer for the LocalTime since it's a singleton.

Something like this:

Serializer.register!(LocalTime);

// Return the singleton instead of deserializing the LocalTime from the serialized data
Serializer.registerDeserializer!(LocalTime, TimeZone)((ref timeZone, serializer, data) {
    timeZone = cast(TimeZone) LocalTime();
});