Custom Serialization

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

    Custom Serialization

    I have a complex object that I need to serialize. Rather than rely on a
    standard routine, which is called during the serialization/deserialization , I
    would like to be able to use my own functions that would convert this object
    into a string and would then write that string to a file. Upon
    deserialization , I need to be able to call another custom function to
    recreate the object.

    I have looked and both implementing the ISerializable Interface and at using
    the On(De)serializi ng(ed) Attributes, but the only seem to set particular
    fields of the object without dealing with particular fields of the object
    without allowing me to record/recreate object as a whole.

    Thanks

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Custom Serialization

    For this, you should not be using serialization. What you really want
    to do is implement a TypeConverter which will convert your object to a
    string.

    The serialization framework is meant to abstract the serialization
    process in the sense that you won't have to worry about formatting, you just
    have to worry about what gets serialized. You could always implement
    ISerializable and then set one field which is the string which really
    repreents your objects, but that just doesn't seem right.

    Here is a link that I found from google with the search phrase
    "implementi ng a TypeConverter":



    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Val" <Val@discussion s.microsoft.com wrote in message
    news:A9CB40BB-A1E4-4F34-B8FA-C21C004B4ED0@mi crosoft.com...
    >I have a complex object that I need to serialize. Rather than rely on a
    standard routine, which is called during the
    serialization/deserialization , I
    would like to be able to use my own functions that would convert this
    object
    into a string and would then write that string to a file. Upon
    deserialization , I need to be able to call another custom function to
    recreate the object.
    >
    I have looked and both implementing the ISerializable Interface and at
    using
    the On(De)serializi ng(ed) Attributes, but the only seem to set particular
    fields of the object without dealing with particular fields of the object
    without allowing me to record/recreate object as a whole.
    >
    Thanks
    >

    Comment

    • Val

      #3
      Re: Custom Serialization

      Well... This is not the only object i am serializing, but the slowest one to
      do so. That is why i wanted to use the custom routine just for it and leave
      the other objects to the system.

      Do you have any suggestions on how to do it?

      Thanks

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      For this, you should not be using serialization. What you really want
      to do is implement a TypeConverter which will convert your object to a
      string.
      >
      The serialization framework is meant to abstract the serialization
      process in the sense that you won't have to worry about formatting, you just
      have to worry about what gets serialized. You could always implement
      ISerializable and then set one field which is the string which really
      repreents your objects, but that just doesn't seem right.
      >
      Here is a link that I found from google with the search phrase
      "implementi ng a TypeConverter":
      >

      >
      Hope this helps.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "Val" <Val@discussion s.microsoft.com wrote in message
      news:A9CB40BB-A1E4-4F34-B8FA-C21C004B4ED0@mi crosoft.com...
      I have a complex object that I need to serialize. Rather than rely on a
      standard routine, which is called during the
      serialization/deserialization , I
      would like to be able to use my own functions that would convert this
      object
      into a string and would then write that string to a file. Upon
      deserialization , I need to be able to call another custom function to
      recreate the object.

      I have looked and both implementing the ISerializable Interface and at
      using
      the On(De)serializi ng(ed) Attributes, but the only seem to set particular
      fields of the object without dealing with particular fields of the object
      without allowing me to record/recreate object as a whole.

      Thanks
      >
      >
      >

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Custom Serialization

        Val,

        My guess is that a custom routine isn't going to help much. The first
        thing you should do is see how much data you are actually serializing, and
        determine how much of it you need to save. Encoding that data into a
        string, or a byte array is still going to be relative to the amount of data
        you need to store.

        Granted, there is an overhead with serialization, in that it uses
        reflection to get field names and whatnot, and a custom implementation of
        serialization will help to reduce that overhead.

        I would recommend looking at the size of the object first, and what you
        are serializing. See if you can't bring that set of data down. Then, look
        at encoding formats.

        To give you a real world example, the DataSet by default serializes its
        members by creating an XML representation internally and then storing that
        in the custom implementation of ISerializable. In .NET 2.0, a binary
        formatting option was offered which was MUCH, MUCH faster.

        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m


        "Val" <Val@discussion s.microsoft.com wrote in message
        news:CBA52F65-57D3-4224-9990-FF34DCEC76A9@mi crosoft.com...
        Well... This is not the only object i am serializing, but the slowest one
        to
        do so. That is why i wanted to use the custom routine just for it and
        leave
        the other objects to the system.
        >
        Do you have any suggestions on how to do it?
        >
        Thanks
        >
        "Nicholas Paldino [.NET/C# MVP]" wrote:
        >
        > For this, you should not be using serialization. What you really
        >want
        >to do is implement a TypeConverter which will convert your object to a
        >string.
        >>
        > The serialization framework is meant to abstract the serialization
        >process in the sense that you won't have to worry about formatting, you
        >just
        >have to worry about what gets serialized. You could always implement
        >ISerializabl e and then set one field which is the string which really
        >repreents your objects, but that just doesn't seem right.
        >>
        > Here is a link that I found from google with the search phrase
        >"implementin g a TypeConverter":
        >>
        >http://www.codeguru.com/columns/vb/article.php/c6529/
        >>
        > Hope this helps.
        >>
        >>
        >--
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >>
        >"Val" <Val@discussion s.microsoft.com wrote in message
        >news:A9CB40B B-A1E4-4F34-B8FA-C21C004B4ED0@mi crosoft.com...
        >I have a complex object that I need to serialize. Rather than rely on a
        standard routine, which is called during the
        serialization/deserialization , I
        would like to be able to use my own functions that would convert this
        object
        into a string and would then write that string to a file. Upon
        deserialization , I need to be able to call another custom function to
        recreate the object.
        >
        I have looked and both implementing the ISerializable Interface and at
        using
        the On(De)serializi ng(ed) Attributes, but the only seem to set
        particular
        fields of the object without dealing with particular fields of the
        object
        without allowing me to record/recreate object as a whole.
        >
        Thanks
        >
        >>
        >>
        >>

        Comment

        • Thomas T. Veldhouse

          #5
          Re: Custom Serialization

          Val <Val@discussion s.microsoft.com wrote:
          Well... This is not the only object i am serializing, but the slowest one to
          do so. That is why i wanted to use the custom routine just for it and leave
          the other objects to the system.
          >
          Do you have any suggestions on how to do it?
          >
          Implement ISerializable and create the required constructor. Construct the
          string that you want serialized from the instance data and store it in a named
          parameter like usual ... rather than storing the instance values into named
          parameters. In the constructor, deserialize the string, break it appart and
          reset the instance variables.


          [Serializable]
          public class TestSerializabl eClass : ISerializable
          {
          private int i = 0;
          private int j = 0;

          public TestSerializabl eClass()
          {

          }

          public TestSerializabl eClass(Serializ ationInfo info,
          StreamingContex t context)
          {
          string serialString = info.GetString( @"DATA");

          i = Convert.ToInt32 (serialString.S plit('-')[0]);
          j = Convert.ToInt32 (serialString.S plit('-')[1]);
          }

          #region ISerializable Members

          public void GetObjectData(S erializationInf o info,
          StreamingContex t context)
          {
          string serialString = string.Format(" {0}-{1}", i, j);

          info.AddValue(@ "DATA", serialString);
          }

          #endregion
          }


          Hope this helps.

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


          Comment

          Working...