Search code examples
oopclassiod

Write/read class objects to/from file, D-Lang


I'm trying to write/read a class object from/to a file.
I'm new to D and I just want to play a little bit around with it.

Is there a Class/Function to write/read an object to/from a file?
I'm looking for something similar to the ObjectOutputStream сlass in Java.

Or do I have to serialize (concatenate) the object's variables as strings in the file?

I have a Movie class and a MovieManager class, which contains a dynamic movie-array.

A Movie object contains just a few strings and integer values.


Solution

  • Extending answer, provided in comment, it is worth explicitly stating, that D does not provide "one true way" of reading/writing objects to/from files, as there can't be a single optimal one. Different considerations about speed, resulting file format, handling references and similar corner cases may results in different serialization strategies.

    That being said, most likely proper serialization library is needed, and, by lucky chance, one of most mature D solutions ("Orange" by Jacob Carlborg https://github.com/jacob-carlborg/orange) is being reviewed right now as a candidate for inclusion into standard library as a std.serialization: newsgroup thread. It may be your best bet.