0

I am using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to Serialize a complex Project object and store it as a .dat file on the local machine. Then I can deserialize this stream and cast to Project to get an exact copy of the original project. The (simplified) code is as follows:

Project project = new Project();
FileStream stream = new FileStream("file.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, project);
stream.Close();

...

stream = new FileStream("file.dat", FileMode.Open);
Project revisitedProject = (Project)formatter.Deserialize(stream);
stream.Close();

and this works for me with no loss of data, as it should. However, this only allows me to store the project on the local machine, so if the device is lost or damaged then so are all of the user's projects. The Project class is too complex to store in a table, so I'm hoping that somebody with more knowledge of Serialization can help me out a bit because I'm very beginner with the concept.

I would like to serialize the project just like I already do.

Then, I want to deserialize the project and cast to a string, this string will be stored in a table.

If the project ever needs to be recovered, that string will be serialized once again and stored in the local machine as a .dat file.

Then, if I deserialize that .dat file and cast it to Project, will I have an exact copy of my original project or would casting and storing it as a string have caused me to lose data?

jarjar27
  • 117
  • 1
  • 14
  • Have you considered a document database like MongoDB, CosmosDB, or RavenDB? Also, is your data _really_ too complicated to store in a relational database? Sure it might require multiple tables, but that's to be expected. – ProgrammingLlama Jul 09 '18 at 01:06
  • JSON? [How do I turn a C# object into a JSON string in .NET?](https://stackoverflow.com/questions/6201529/how-do-i-turn-a-c-sharp-object-into-a-json-string-in-net). [JSON Serialization And Deserialization](https://www.c-sharpcorner.com/article/json-serialization-and-deserialization-in-c-sharp/). – Jimi Jul 09 '18 at 01:07

1 Answers1

2

When you have your .dat file, you already have a serialized version of the complex object, no need to turn this into string. All modern databases support storing data as a blob: that is, data without structure that would be understood by the database engine. You can store the content of your .dat file(s) in a database, in a blob field. Different databases call this data type in a different way, e.g. MSSQL calls it varbinary.

As an addition, if you insist converting to string, consider converting the binary using base64. It will make it safe both encoding and decoding without codepage and language encoding concerns.

PepitoSh
  • 1,774
  • 14
  • 13
  • While that's true, I'd argue that serializing a class to store in a relational database is a bad practice. +1 since this is the correct answer as far as how to store the serialized data. – ProgrammingLlama Jul 09 '18 at 01:59
  • 2
    @john: In general I agree. We don't know the exact scenario. Maybe it requires way too much efforts for the relatively low cost of doing binary serialization. There may be security concerns and a wrong approach to address it. Maybe the .dat file needs to be shared without sharing a database. OP said, it is for backup purposes. I would think of versioning, too. – PepitoSh Jul 09 '18 at 02:03