serialize/deserialize object into string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Val

    serialize/deserialize object into string

    How can I serialize/deserialize an object into a string. Existing examples
    seem to be showing this operation for files only.

    Thansk
  • Peter Bromberg [C# MVP]

    #2
    RE: serialize/deserialize object into string

    What you are really asking is how to convert a stream (e.g. MemoryStream) to
    a string. If you are using the BinaryFormatter the MemoryStream you serialize
    to or from can be converted to / from a string as follows:

    Stream to string:

    byte[] b = MyMemoryStream. ToArray();
    string s = System.Text.Enc oding.UTF8.GetS tring(b);

    String to stream:

    string s = "whatever";
    byte[] b = System.Text.Enc oding.UTF8.GetB ytes(s);
    MemoryStream ms = new MemoryStream(b) ;

    Cheers.
    Peter
    --
    Co-founder, Eggheadcafe.com developer portal:

    UnBlog:





    "Val" wrote:
    How can I serialize/deserialize an object into a string. Existing examples
    seem to be showing this operation for files only.
    >
    Thansk

    Comment

    • Thomas T. Veldhouse

      #3
      Re: serialize/deserialize object into string

      Val <Val@discussion s.microsoft.com wrote:
      How can I serialize/deserialize an object into a string. Existing examples
      seem to be showing this operation for files only.
      >
      using System.IO;
      using System.Runtime. Serialization;
      using System.Runtime. Serialization.F ormatters.Binar y;

      ....

      MemoryStream memoryStream = new MemoryStream();
      BinaryFormatter binaryFormatter = new BinaryFormatter ();
      binaryFormatter .Serialize(memo ryStream, targetObject);
      string str = System.Convert. ToBase64String( memoryStream.To Array());

      where targetObject is the object that you are trying to serialize.


      --
      Thomas T. Veldhouse
      Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

      Comment

      • veera sekhar kota

        #4
        RE: serialize/deserialize object into string

        Hey

        Just try with this code.. it seriliaze an object to a string..

        using System;
        using System.IO;
        using System.Runtime. Serialization;
        using System.Runtime. Serialization.F ormatters.Binar y;

        namespace SeriliazationEx
        {
        [Serializable()] //Set this attribute to all the classes that want to
        serialize
        public class Employee
        {
        public int EmpId;
        public string EmpName;

        //Default constructor
        public Employee()
        {
        EmpId = 0;
        EmpName = null;
        }
        }

        public class ObjSerial
        {
        public static void Main(String[] args)
        {
        //Create a new Employee object
        Employee mp = new Employee();
        mp.EmpId = 10;
        mp.EmpName = "Omkumar";

        //Add code below for serialization
        string s =
        System.Text.Enc oding.UTF8.GetS tring(getByteAr rayWithObject(m p));
        Console.WriteLi ne(s);
        }
        public static byte[] getByteArrayWit hObject(Object o)
        {
        /*

        1) Create a new MemoryStream class with the CanWrite property set to true
        (should be by default, using the default constructor).

        2) Create a new instance of the BinaryFormatter class.

        3) Pass the MemoryStream instance and your object to be serialized to the
        Serialize method of the BinaryFormatter class.

        4) Call the ToArray method on the MemoryStream class to get a byte array
        with the serialized data.

        */

        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf1 = new BinaryFormatter ();
        bf1.Serialize(m s, o);
        return ms.ToArray();
        }
        }
        }



        Veera.


        "Val" wrote:
        How can I serialize/deserialize an object into a string. Existing examples
        seem to be showing this operation for files only.
        >
        Thansk

        Comment

        • Jon Skeet [C# MVP]

          #5
          RE: serialize/deserialize object into string

          Peter Bromberg [C# MVP] <pbromberg@yaho o.nospammin.com wrote:
          What you are really asking is how to convert a stream (e.g. MemoryStream) to
          a string. If you are using the BinaryFormatter the MemoryStream you serialize
          to or from can be converted to / from a string as follows:
          >
          Stream to string:
          >
          byte[] b = MyMemoryStream. ToArray();
          string s = System.Text.Enc oding.UTF8.GetS tring(b);
          >
          String to stream:
          >
          string s = "whatever";
          byte[] b = System.Text.Enc oding.UTF8.GetB ytes(s);
          MemoryStream ms = new MemoryStream(b) ;
          That's a way which is almost guaranteed to lose data. Serialization
          with BinaryFormatter produces opaque binary data, which may very well
          not be a valid UTF-8 encoded string.

          To convert arbitrary binary data to a string and back, I'd use
          Convert.ToBase6 4String and Convert.FromBas e64String.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Thomas T. Veldhouse

            #6
            Re: serialize/deserialize object into string

            Jon Skeet [C# MVP] <skeet@pobox.co mwrote:
            Peter Bromberg [C# MVP] <pbromberg@yaho o.nospammin.com wrote:
            >What you are really asking is how to convert a stream (e.g. MemoryStream) to
            >a string. If you are using the BinaryFormatter the MemoryStream you serialize
            >to or from can be converted to / from a string as follows:
            >>
            >Stream to string:
            >>
            >byte[] b = MyMemoryStream. ToArray();
            >string s = System.Text.Enc oding.UTF8.GetS tring(b);
            >>
            >String to stream:
            >>
            >string s = "whatever";
            >byte[] b = System.Text.Enc oding.UTF8.GetB ytes(s);
            >MemoryStream ms = new MemoryStream(b) ;
            >
            That's a way which is almost guaranteed to lose data. Serialization
            with BinaryFormatter produces opaque binary data, which may very well
            not be a valid UTF-8 encoded string.
            >
            To convert arbitrary binary data to a string and back, I'd use
            Convert.ToBase6 4String and Convert.FromBas e64String.
            >
            I posted a code snippet using just what you discribe ... look elsewhere in
            this thread. Encoding binary data as string often leaves "nulls" and other
            characters that string code often considers "end of string" (like /0 in
            ASCII), and thus should be avoided ... Base64 does a fine job of avioding it.

            --
            Thomas T. Veldhouse
            Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

            Comment

            • Peter Bromberg [C# MVP]

              #7
              RE: serialize/deserialize object into string

              Jon,
              Good point. It's so easy when you are focused on spitting out a quick
              example for somebody, to pass over some basics.
              Peter

              --
              Co-founder, Eggheadcafe.com developer portal:

              UnBlog:





              "Jon Skeet [C# MVP]" wrote:
              Peter Bromberg [C# MVP] <pbromberg@yaho o.nospammin.com wrote:
              What you are really asking is how to convert a stream (e.g. MemoryStream) to
              a string. If you are using the BinaryFormatter the MemoryStream you serialize
              to or from can be converted to / from a string as follows:

              Stream to string:

              byte[] b = MyMemoryStream. ToArray();
              string s = System.Text.Enc oding.UTF8.GetS tring(b);

              String to stream:

              string s = "whatever";
              byte[] b = System.Text.Enc oding.UTF8.GetB ytes(s);
              MemoryStream ms = new MemoryStream(b) ;
              >
              That's a way which is almost guaranteed to lose data. Serialization
              with BinaryFormatter produces opaque binary data, which may very well
              not be a valid UTF-8 encoded string.
              >
              To convert arbitrary binary data to a string and back, I'd use
              Convert.ToBase6 4String and Convert.FromBas e64String.
              >
              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too
              >

              Comment

              Working...