Deserialization

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

    #1

    Deserialization

    Hi

    I'm wondering if there's a technique to deserialize objects which were
    serialized with an earlier version of the class. Example:

    // version 1
    class
    {
    string myString;
    int myInt;
    }

    is serialized to myobject.xml

    In an update version of my app, I need to deserialize the myobject.xml, but
    my class has changed due to new app requirements:

    // version 2
    class
    {
    string myString;
    int myInt;
    bool myBool;
    }

    Now, it won't deserialize anymore. In order to guarantee product update
    compatibility, do I have to maintain all previous versions of my serialized
    classes in my app and do try-catch-try-catch blocks until the correct
    version can be read?

    Any hints are appreciated

    Urs


  • James

    #2
    Re: Deserialization


    "Urs Vogel" <ursvogel@yahoo .com> wrote in message
    news:u8kxWMgRDH A.1324@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hi
    >
    > I'm wondering if there's a technique to deserialize objects which were
    > serialized with an earlier version of the class. Example:
    >
    > // version 1
    > class
    > {
    > string myString;
    > int myInt;
    > }
    >
    > is serialized to myobject.xml
    >
    > In an update version of my app, I need to deserialize the myobject.xml,[/color]
    but[color=blue]
    > my class has changed due to new app requirements:
    >
    > // version 2
    > class
    > {
    > string myString;
    > int myInt;
    > bool myBool;
    > }
    >
    > Now, it won't deserialize anymore. In order to guarantee product update
    > compatibility, do I have to maintain all previous versions of my[/color]
    serialized[color=blue]
    > classes in my app and do try-catch-try-catch blocks until the correct
    > version can be read?
    >
    > Any hints are appreciated
    >
    > Urs
    >
    >
    > .
    >[/color]


    There is no way to do this as you really want. But take a look at the
    Header[] arguments of the serialize/deserialize methods - ,these headers
    will keep track of all objects within the file etc, allowing many objects to
    be serialized to the same file - maybe you could delete old object types and
    update them with the new ones by catching the headers callback - I haven't
    thought this through much though...

    James


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Deserialization

      James,

      This isn't completely true. To handle version differences, it is
      recommended that you implement the ISerializable interface on your object,
      and then handle the information that is in the SerializationIn fo instance
      that is lacking due to version differences.

      Hope this helps.

      --
      - Nicholas Paldino [.NET/C# MVP]
      - nicholas.paldin o@exisconsultin g.com

      "James" <James@NOSPAMje nkins2040.frees erve.co.uk> wrote in message
      news:ecAosXgRDH A.560@TK2MSFTNG P10.phx.gbl...[color=blue]
      >
      > "Urs Vogel" <ursvogel@yahoo .com> wrote in message
      > news:u8kxWMgRDH A.1324@TK2MSFTN GP11.phx.gbl...[color=green]
      > > Hi
      > >
      > > I'm wondering if there's a technique to deserialize objects which were
      > > serialized with an earlier version of the class. Example:
      > >
      > > // version 1
      > > class
      > > {
      > > string myString;
      > > int myInt;
      > > }
      > >
      > > is serialized to myobject.xml
      > >
      > > In an update version of my app, I need to deserialize the myobject.xml,[/color]
      > but[color=green]
      > > my class has changed due to new app requirements:
      > >
      > > // version 2
      > > class
      > > {
      > > string myString;
      > > int myInt;
      > > bool myBool;
      > > }
      > >
      > > Now, it won't deserialize anymore. In order to guarantee product update
      > > compatibility, do I have to maintain all previous versions of my[/color]
      > serialized[color=green]
      > > classes in my app and do try-catch-try-catch blocks until the correct
      > > version can be read?
      > >
      > > Any hints are appreciated
      > >
      > > Urs
      > >
      > >
      > > .
      > >[/color]
      >
      >
      > There is no way to do this as you really want. But take a look at the
      > Header[] arguments of the serialize/deserialize methods - ,these headers
      > will keep track of all objects within the file etc, allowing many objects[/color]
      to[color=blue]
      > be serialized to the same file - maybe you could delete old object types[/color]
      and[color=blue]
      > update them with the new ones by catching the headers callback - I haven't
      > thought this through much though...
      >
      > James
      >
      >[/color]


      Comment

      • James

        #4
        Re: Deserialization


        "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
        in message news:u8IDFnhRDH A.1804@TK2MSFTN GP11.phx.gbl...[color=blue]
        > James,
        >
        > This isn't completely true. To handle version differences, it is
        > recommended that you implement the ISerializable interface on your object,
        > and then handle the information that is in the SerializationIn fo instance
        > that is lacking due to version differences.
        >
        > Hope this helps.
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - nicholas.paldin o@exisconsultin g.com
        >
        > "James" <James@NOSPAMje nkins2040.frees erve.co.uk> wrote in message
        > news:ecAosXgRDH A.560@TK2MSFTNG P10.phx.gbl...[color=green]
        > >
        > > "Urs Vogel" <ursvogel@yahoo .com> wrote in message
        > > news:u8kxWMgRDH A.1324@TK2MSFTN GP11.phx.gbl...[color=darkred]
        > > > Hi
        > > >
        > > > I'm wondering if there's a technique to deserialize objects which were
        > > > serialized with an earlier version of the class. Example:
        > > >
        > > > // version 1
        > > > class
        > > > {
        > > > string myString;
        > > > int myInt;
        > > > }
        > > >
        > > > is serialized to myobject.xml
        > > >
        > > > In an update version of my app, I need to deserialize the[/color][/color][/color]
        myobject.xml,[color=blue][color=green]
        > > but[color=darkred]
        > > > my class has changed due to new app requirements:
        > > >
        > > > // version 2
        > > > class
        > > > {
        > > > string myString;
        > > > int myInt;
        > > > bool myBool;
        > > > }
        > > >
        > > > Now, it won't deserialize anymore. In order to guarantee product[/color][/color][/color]
        update[color=blue][color=green][color=darkred]
        > > > compatibility, do I have to maintain all previous versions of my[/color]
        > > serialized[color=darkred]
        > > > classes in my app and do try-catch-try-catch blocks until the correct
        > > > version can be read?
        > > >
        > > > Any hints are appreciated
        > > >
        > > > Urs
        > > >
        > > >
        > > > .
        > > >[/color]
        > >
        > >
        > > There is no way to do this as you really want. But take a look at the
        > > Header[] arguments of the serialize/deserialize methods - ,these[/color][/color]
        headers[color=blue][color=green]
        > > will keep track of all objects within the file etc, allowing many[/color][/color]
        objects[color=blue]
        > to[color=green]
        > > be serialized to the same file - maybe you could delete old object types[/color]
        > and[color=green]
        > > update them with the new ones by catching the headers callback - I[/color][/color]
        haven't[color=blue][color=green]
        > > thought this through much though...
        > >
        > > James
        > >
        > >[/color]
        >
        >[/color]

        ok - as you on this thread can you tell me a good use for the header[] is? -
        Like I said in post, I have not give this much thought and obviously what
        you say about implementing the interface is correct, but what use for the
        header[] - is it simply to retrieve specific objects etc?

        thanks


        Comment

        • Urs Vogel

          #5
          Re: Deserialization

          Kevin

          The problem exists for both, binary and soap serialization. XSL still would
          require some additional deliverables for updates, very hard to control.
          Users expect updateds versions to run smoothly without the requirement of
          converting data. I think Nicholas Paldinos comments are leading the right
          way.

          Thanks,
          Urs

          "Kevin Conroy" <kconroy1@proli st.com> schrieb im Newsbeitrag
          news:ujCxb1gRDH A.1556@TK2MSFTN GP10.phx.gbl...[color=blue][color=green]
          > > In an update version of my app, I need to deserialize the myobject.xml,[/color]
          > but[color=green]
          > > my class has changed due to new app requirements:[/color]
          >
          > Since you using Xml serialization, you could always create an XSL[/color]
          stylesheet[color=blue]
          > which can transform from the old format to the new format. Perhaps[/color]
          defining[color=blue]
          > a default value of false to the boolean value or something. I could be
          > mistaken, but I don't think that you have to have every single field[/color]
          defined[color=blue]
          > when you deserialize. The Xml Serialization portion of the .NET framework
          > will just leave unserialized properties alone.
          >
          > Of course this is an issue if your class is expecting a value to be[/color]
          defined[color=blue]
          > for the boolean field (or what have you). This is why I'd suggest using an
          > Xsl.
          >
          > Or, you could make another version of the object that is more akin to the
          > old style. This object could then automatically create a version of the[/color]
          new[color=blue]
          > object and set up the values correctly. This is like a programmatic[/color]
          version[color=blue]
          > of doing an Xsl transform on the serialized Xml and thus is only a better
          > solution over Xsl if you need to perform a complex operation to convert[/color]
          the[color=blue]
          > old serialization to the new format and cannot do it in Xsl.
          >
          > Just my 2 cents.
          >
          >
          > Kevin Conroy
          > Programmer/Analyst
          >
          >[/color]


          Comment

          • Urs Vogel

            #6
            Re: Deserialization

            Thanks for your help, I will follow this track

            Urs

            "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>
            schrieb im Newsbeitrag news:u8IDFnhRDH A.1804@TK2MSFTN GP11.phx.gbl...[color=blue]
            > James,
            >
            > This isn't completely true. To handle version differences, it is
            > recommended that you implement the ISerializable interface on your object,
            > and then handle the information that is in the SerializationIn fo instance
            > that is lacking due to version differences.
            >
            > Hope this helps.
            >
            > --
            > - Nicholas Paldino [.NET/C# MVP]
            > - nicholas.paldin o@exisconsultin g.com
            >
            > "James" <James@NOSPAMje nkins2040.frees erve.co.uk> wrote in message
            > news:ecAosXgRDH A.560@TK2MSFTNG P10.phx.gbl...[color=green]
            > >
            > > "Urs Vogel" <ursvogel@yahoo .com> wrote in message
            > > news:u8kxWMgRDH A.1324@TK2MSFTN GP11.phx.gbl...[color=darkred]
            > > > Hi
            > > >
            > > > I'm wondering if there's a technique to deserialize objects which were
            > > > serialized with an earlier version of the class. Example:
            > > >
            > > > // version 1
            > > > class
            > > > {
            > > > string myString;
            > > > int myInt;
            > > > }
            > > >
            > > > is serialized to myobject.xml
            > > >
            > > > In an update version of my app, I need to deserialize the[/color][/color][/color]
            myobject.xml,[color=blue][color=green]
            > > but[color=darkred]
            > > > my class has changed due to new app requirements:
            > > >
            > > > // version 2
            > > > class
            > > > {
            > > > string myString;
            > > > int myInt;
            > > > bool myBool;
            > > > }
            > > >
            > > > Now, it won't deserialize anymore. In order to guarantee product[/color][/color][/color]
            update[color=blue][color=green][color=darkred]
            > > > compatibility, do I have to maintain all previous versions of my[/color]
            > > serialized[color=darkred]
            > > > classes in my app and do try-catch-try-catch blocks until the correct
            > > > version can be read?
            > > >
            > > > Any hints are appreciated
            > > >
            > > > Urs
            > > >
            > > >
            > > > .
            > > >[/color]
            > >
            > >
            > > There is no way to do this as you really want. But take a look at the
            > > Header[] arguments of the serialize/deserialize methods - ,these[/color][/color]
            headers[color=blue][color=green]
            > > will keep track of all objects within the file etc, allowing many[/color][/color]
            objects[color=blue]
            > to[color=green]
            > > be serialized to the same file - maybe you could delete old object types[/color]
            > and[color=green]
            > > update them with the new ones by catching the headers callback - I[/color][/color]
            haven't[color=blue][color=green]
            > > thought this through much though...
            > >
            > > James
            > >
            > >[/color]
            >
            >[/color]


            Comment

            Working...