(De)Serializing interfaces
nims via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Aug 23 01:15:06 PDT 2015
On Sunday, 23 August 2015 at 03:09:03 UTC, Rikki Cattermole wrote:
> Anyway to summise why D doesn't yet have something akin to Java
> or C#. Simply put, we generally work with the actual type not
> an interface. So libraries like Orange can
> serialize/deserialize with great certainty that it got
> everything.
>
> However if you need any help with making such a library, please
> let me know!
Actually I'm coming from C++ but I know a little bit about C# so
your explanation was helpful anyway! Thanks!
As I really need a working serialization library I'll try making
one myself now. However I'm not very experienced with D in
general and its reflection in particular so I could use some help.
I think in order to keep it simple I'll just have the user to
write a function called Serialize() which calls the serializer
which then just writes the values one after the other in a binary
file (and, of course, for arrays and strings the length too). As
all classes implement an interface called Serializable (with the
function Serialize()), finding the right class to serialize won't
be too hard (at least I think so). However then we have to make
sure that we deserialize (instantiate) the right class.
Does the runtime have a function which give you something like a
unique type id and another one which instantiates the type with
the given id?
Something like:
int id = _magic_runtime_functions.getUniqueTypeId(typeid(T))
Serializable t = _magic_runtime_functions.createTypeOfId(...)
In order to make this clear I wrote some (untested and
unfinished) code and pushed it into GitHub:
https://github.com/nims1/interface-serialization-d/blob/master/Serializer.d
I know it still has a lot of problems (hopefully, D has a better
way of writing binary files into memory...). It's just a quick
draft.
More information about the Digitalmars-d-learn
mailing list