Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing/deserializing with memory stream

I'm having an issue with serializing using memory stream. Here is my code:

/// <summary> /// serializes the given object into memory stream /// </summary> /// <param name="objectType">the object to be serialized</param> /// <returns>The serialized object as memory stream</returns> public static MemoryStream SerializeToStream(object objectType) {     MemoryStream stream = new MemoryStream();     IFormatter formatter = new BinaryFormatter();     formatter.Serialize(stream, objectType);     return stream; }  /// <summary> /// deserializes as an object /// </summary> /// <param name="stream">the stream to deserialize</param> /// <returns>the deserialized object</returns> public static object DeserializeFromStream(MemoryStream stream) {     IFormatter formatter = new BinaryFormatter();     stream.Seek(0, SeekOrigin.Begin);     object objectType = formatter.Deserialize(stream);     return objectType; }  

The error I'm getting is as follow: stream is not a valid binary format. The starting contents (in bytes) are: blah....

I'm not exactly sure what is causing the error. Any help would be greatly appreciated.

Example of the call:

Dog myDog = new Dog(); myDog.Name= "Foo"; myDog.Color = DogColor.Brown;  MemoryStream stream = SerializeToStream(myDog)  Dog newDog = (Dog)DeserializeFromStream(stream); 
like image 749
gng Avatar asked Apr 30 '12 20:04

gng


People also ask

What does serializing and Deserializing mean?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

What is serializing and Deserializing in Python?

Object serialization is the process of converting state of an object into byte stream. This byte stream can further be stored in any file-like object such as a disk file or memory stream. It can also be transmitted via sockets etc. Deserialization is the process of reconstructing the object from the byte stream.

What is serializing and Deserializing JavaScript?

Summary. Serialization takes an in-memory data structure and converts it into a series of bytes that can be stored and transferred. Deserialization takes a series of bytes and converts it to an in-memory data structure that can be consumed programmatically.


1 Answers

This code works for me:

public void Run() {     Dog myDog = new Dog();     myDog.Name= "Foo";     myDog.Color = DogColor.Brown;      System.Console.WriteLine("{0}", myDog.ToString());      MemoryStream stream = SerializeToStream(myDog);      Dog newDog = (Dog)DeserializeFromStream(stream);      System.Console.WriteLine("{0}", newDog.ToString()); } 

Where the types are like this:

[Serializable] public enum DogColor {     Brown,     Black,     Mottled }  [Serializable] public class Dog {     public String Name     {         get; set;     }      public DogColor Color     {         get;set;     }      public override String ToString()     {         return String.Format("Dog: {0}/{1}", Name, Color);     } } 

and the utility methods are:

public static MemoryStream SerializeToStream(object o) {     MemoryStream stream = new MemoryStream();     IFormatter formatter = new BinaryFormatter();     formatter.Serialize(stream, o);     return stream; }  public static object DeserializeFromStream(MemoryStream stream) {     IFormatter formatter = new BinaryFormatter();     stream.Seek(0, SeekOrigin.Begin);     object o = formatter.Deserialize(stream);     return o; } 
like image 91
Cheeso Avatar answered Sep 22 '22 18:09

Cheeso