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?

9
  • Serialization == converting an object into a format that can be stored for reconstruction. So what exactly is your question? Commented Aug 25, 2014 at 12:12
  • Given ObjectOutputStream requires the object to save to implement Serializable, 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? Commented Aug 25, 2014 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? Commented Aug 25, 2014 at 12:13
  • 1
    Only the BinaryFormatter requires [Serializable]. 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. Commented Aug 25, 2014 at 12:19
  • 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. Commented Aug 25, 2014 at 12:30

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.