0

Edit: The original question was made out of a misunderstanding, and has been updated.

Coming from other languages it seems odd that C# does not seem to have a simple way to dump things like objects and components straight to file.

Lets take java as an example, where I can dump any object to file and load with no apparent restrictions:

  //Save object to file:
  FileOutputStream saveFile = new FileOutputStream(---filePath---);
  ObjectOutputStream save = new ObjectOutputStream(saveFile);
  save.writeObject(---YorObjectHere---);

  //Load object from file
  FileInputStream saveFile = new FileInputStream(---filePath---);
  ObjectInputStream save = new ObjectInputStream(saveFile);
  ---ObjectType--- loadedObject = (---ObjectType---) save.readObject();

Can this sort of thing be easily achieved in C#?

I have tried the standard method of serialization:

  //Save object to file:
  IFormatter formatterSave = new BinaryFormatter();
  Stream streamSave = new FileStream(---filePath---, FileMode.Create, FileAccess.Write, FileShare.None);
  formatterSave.Serialize(streamSave, ---ObjectToSave---);

  //Load object from file
  IFormatter formatterLoad = new BinaryFormatter();
  Stream streamLoad = new FileStream(---filePath---, FileMode.Open, FileAccess.Read, FileShare.Read);
  ---ObjectType--- ---LoadedObjectName--- = (---ObjectType---)formatterLoad.Deserialize(streamLoad);

While this method is quite simple to do, it does not always work with existing or locked code because the [serializable] tag cannot always be added.

So what is the best alternate for serialization?


Thanks to comments I tried the XML method, but it did not work either because it cannot serialize an ArrayList as shown on MSDN

  //Save object to file:
  var serializerSave = new XmlSerializer(typeof(objectType));
  writer = new StreamWriter(filePath, append);
  serializerSave.Serialize(writer, objectToWrite);

  //Load object from file
  var serializerLoad = new XmlSerializer(typeof(objectType));
  reader = new StreamReader(filePath);
  return (T)serializerLoad.Deserialize(reader);

It looks like JSON is the only way to do it easily, or is there another alternate way to serialize with normal C# libraries without needing massive amounts of code?

sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • Serialization `==` converting an object into a format that can be stored for reconstruction. So what exactly is your question? – vgru Aug 25 '14 at 12:12
  • Given [`ObjectOutputStream` requires the object to save to implement `Serializable`](http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html), I don't really see the difference between the two code blocks apart from the Java counterpart supporting serialized serialization as opposed to .NET where one file contains one object. Why would you _not_ want to used the built in method of serialization? – CodeCaster Aug 25 '14 at 12:13
  • Isn't ObjectOutputStream a serializer? I'm missing the concern with using .NET's version of ObjectOutputStream, the BinaryFormatter. Is it the additional control you get with the BinaryFormatter that you take issue with? – Rob Epstein Aug 25 '14 at 12:13
  • @Groo Well said. The question would be better titled asked as "while not requiring default serialization and the use of the `[Serializable]` tag" – sorifiend Aug 25 '14 at 12:14
  • @CodeCaster correct, but how does that work with existing or locked code that I cannot add the [Serializable] tag too. – sorifiend Aug 25 '14 at 12:16
  • @sorifiend: of course there are ways. Decide which format you want an go with it. XML? Supported out of the box. JSON? Check. Protocol buffers? Use an open source library. Use Google or the search box on Stack Overflow and you'll get plenty of resources. – vgru Aug 25 '14 at 12:16
  • 1
    Only the [`BinaryFormatter` requires `[Serializable]`](http://stackoverflow.com/questions/2982376/why-is-serializable-attribute-required-for-an-object-to-be-serialized). If you want to binary-serialize classes that don't carry that attribute and you understand the consequences, use different ways of serialization (like XML/JSON/... mentioned above, serializing only public properties) or [using a surrogate](http://stackoverflow.com/questions/2461445/how-to-make-a-class-serializable-without-serializable-attibute-in-net). – CodeCaster Aug 25 '14 at 12:19
  • @CodeCaster wow, that was a poor misunderstanding on my part. Thanks for clearing that up, I can close the question or if prefer you can re-post your comment as an answer, I will accept it for anyone else to see who may have the same misunderstanding. – sorifiend Aug 25 '14 at 12:22
  • 1
    @sorifiend: not only that, but `BinaryFormatter` is pretty much the worst possible approach you can take for serialization (closed format, no versioning support, slowness). Which is sad given its apparent "first-class citizenship" within .NET. – vgru Aug 25 '14 at 12:30

1 Answers1

1

Thanks to those who commented and pointed me in the right direction.

Although its not covered in the basic libraries, using JSON looks like the best alternate in this situation, I plan to use Json.NET, it can be found here: http://www.nuget.org/packages/Newtonsoft.Json

Another good option is Google Gson: https://code.google.com/p/google-gson/

sorifiend
  • 5,927
  • 1
  • 28
  • 45