473,769 Members | 7,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Backward compatibility issue of Serialization

Hi everybody,

I'm planning to use serialization to persist an object (and possibly
its child objects) in my application. However, I'm concerned about the
backward compatibility issue. I'm evaluating if we can easily resolve
this issue.

For example, I have a class MyClass consisting of 100 fields.

[Serializable]
public class MyClass
{
private int oldField1;
private int oldField2;
...
private int oldField100;
}

The basic serialization can be done simply by adding [Serializable]
attribute to the class. Suppose an object of this class has been
serialized to MyClass.bin using BinaryFormatter . Later, we have a new
version of this class, with the addition of newField101.

[Serializable]
public class MyClass
{
private int oldField1;
private int oldField2;
...
private int oldField100;
private int newField101;
}

If we use the basic deserialization to retrieve this object, the
following runtime exception will be thrown.

System.Runtime. Serialization.S erializationExc eption: Possible Version
mismatch. Type MyClass has 101 members, number of members deserialized
is 100.

My question is how I can resolve this problem. I attempt to solve this
problem using the following approach.

[Serializable]
public class MyClass: ISerializable
{
private int oldField1;
private int oldField2;
...
private int oldField100;

protected MyClass(Seriali zationInfo info, StreamingContex t context)
{
oldField1 = info.GetInt32(" oldField1");
oldField2 = info.GetInt32(" oldField2");
...
oldField3 = info.GetInt32(" oldField100");

if (info.MemberCou nt == 100)
newField101 = 0;
else
newField101 = info.GetInt32(" newField101");
}
public virtual void GetObjectData(S erializationInf o info,
StreamingContex t context)
{
info.AddValue(" oldField1", oldField1);
info.AddValue(" oldField2", oldField2);
...
info.AddValue(" oldField100", oldField100);
info.AddValue(" newField100", newField101);

}
}

Does the above approach always solve the backward compatibility issue
of (de-)serialization?

Even if it does, it is a hassle that I need to list all 100 fields (or
101 fields) in the MyClass constructor and GetObjectData. Is there an
easier way to achieve backward compatibility?

Even if the class structure is not changed, do you think that a new
version of .NET framework will create similar compatibility problem?

Thanks
Dominic
Nov 15 '05 #1
2 6210
So you have two things to worry about:
1. MyClass is going to change
2. MS is going to change the serialization format

1. Don't change MyClass at all, instead derive a new type from MyClass and use
that to add new fields. If you still want your application to return MyNewClass
types, then you can use a SerializationBi nder to map instances of MyClass in the
stream to MyNewClass. There are many ways to control this entire process, and
some are more scalable/complex versus easy/quick to implement.
2. MS isn't going to change the format. If they do, the BinaryFormatter should
be able to handle both old and new formats.

What I would be worried about is contained in my blog entry:
http://weblogs.asp.net/justin_rogers.../02/66508.aspx

Check that out and figure if you still want to use the BinaryFormatter
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Dominic" <do****@hotmail .com> wrote in message
news:2b******** *************** ***@posting.goo gle.com...
Hi everybody,

I'm planning to use serialization to persist an object (and possibly
its child objects) in my application. However, I'm concerned about the
backward compatibility issue. I'm evaluating if we can easily resolve
this issue.

For example, I have a class MyClass consisting of 100 fields.

[Serializable]
public class MyClass
{
private int oldField1;
private int oldField2;
...
private int oldField100;
}

The basic serialization can be done simply by adding [Serializable]
attribute to the class. Suppose an object of this class has been
serialized to MyClass.bin using BinaryFormatter . Later, we have a new
version of this class, with the addition of newField101.

[Serializable]
public class MyClass
{
private int oldField1;
private int oldField2;
...
private int oldField100;
private int newField101;
}

If we use the basic deserialization to retrieve this object, the
following runtime exception will be thrown.

System.Runtime. Serialization.S erializationExc eption: Possible Version
mismatch. Type MyClass has 101 members, number of members deserialized
is 100.

My question is how I can resolve this problem. I attempt to solve this
problem using the following approach.

[Serializable]
public class MyClass: ISerializable
{
private int oldField1;
private int oldField2;
...
private int oldField100;

protected MyClass(Seriali zationInfo info, StreamingContex t context)
{
oldField1 = info.GetInt32(" oldField1");
oldField2 = info.GetInt32(" oldField2");
...
oldField3 = info.GetInt32(" oldField100");

if (info.MemberCou nt == 100)
newField101 = 0;
else
newField101 = info.GetInt32(" newField101");
}
public virtual void GetObjectData(S erializationInf o info,
StreamingContex t context)
{
info.AddValue(" oldField1", oldField1);
info.AddValue(" oldField2", oldField2);
...
info.AddValue(" oldField100", oldField100);
info.AddValue(" newField100", newField101);

}
}

Does the above approach always solve the backward compatibility issue
of (de-)serialization?

Even if it does, it is a hassle that I need to list all 100 fields (or
101 fields) in the MyClass constructor and GetObjectData. Is there an
easier way to achieve backward compatibility?

Even if the class structure is not changed, do you think that a new
version of .NET framework will create similar compatibility problem?

Thanks
Dominic

Nov 15 '05 #2
JS
I'm in the middle of a fairly large application and was using
serialization to save object data. However, this became a huge
headache for a lot of reasons. So I changed all of my persistence to
XML. Note, I did not use an XML serializer, I just added Save/Load
methods to the classes I needed to persist.

This has worked out great. No more SoapFormatter problems. No more
versioning problems.
From my experience I would recommend avoiding serialization for saving

data. Serialization is still needed for remoted-by-value objects, of
course, but I found that I really don't need to implement ISerializable
any more.

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
481
by: someone | last post by:
Suppose that I have a class in an assembly that is delivered to the user, what can I do to change the class so that it doesn't break the binary compatibility? That is, user application can run with recompiling and relinking. I know that if I define an interface, and only expose the interface but not the class which implments the interface, I can add a data member to the class without breaking the binary compatibility. If the class...
1
1657
by: Vyacheslav Lanovets | last post by:
Hello, All! Installed VS 2005... What is annoying is that project files are not backward compatible. For instance, EVC4 vcp files are backward compatible with EVC3 and can be used simultaneously (moreover I could run EVC3 and EVC4 with the same workspace at the same time). vcproj files are in xml, why not to allocate separate node to store
2
1885
by: Neal Becker | last post by:
How can I write code to take advantage of new decorator syntax, while allowing backward compatibility? I almost want a preprocessor. #if PYTHON_VERSION >= 2.4 @staticmethod ....
1
1921
by: Dev | last post by:
Dear Friends, I have created VC++.NET dll by VS.NET2003.Is it possible to use this dll(vs.net2003) into C# version (Vs.net2002)? Is there backward compatibility? If so ..How do to this?..If anyone knows ..please let me know. Thanks, Dev
1
1667
by: MLibby | last post by:
I'm a Netscape newbie and am using it for backward compatibility testing. How do I debug javascript in Netscape? I set Netscape as the default debugger (design mode | file | Browse With) and I am able to debug code behind. However, when I try to debug the .js file the debugger doesn't hit the breakpoints! Any ideas? Mike --
3
3966
by: Madhu | last post by:
We are having a dll backward compatibility issue since we migrated to .NET 2005. As part of our product, we build a dll which is used by other products. Everything was fine when we were build it on VC6. We upgraded our compiler to ..NET 2005 for our new version. The new dll fails to load, we get a runtime error: R6034. We managed to work it around by creating a manifest file for the loading executable, but this is not an acceptable work...
0
1437
by: bhargav mandlem | last post by:
hi friends i have created VC++.NET dll by VS.NET2003.Is it possible to use this dll(vs.net2003) into C# version (Vs.net2002)? Is there backward compatibility? If so ..How do to this?..If anyone knows ..please let me know. Regards Bhargav
2
1841
by: Ernesto Bascón | last post by:
Hi: I've read some about the pimpl idiom and I know that it provides safe ABI backward compatibility on shared libraries. Let's consider the following definitions: template <class T> class A {
7
1851
by: Ernesto Bascón | last post by:
Hi everybody: I want to develop a library that uses heavily templates. Is there an idiom or some tips about things that I should care of in order to provide backward compatibility with my library? (i. e., if I release new versions of my library, what things must I consider to allow the applications that use it, run with no recompilation? There is some idiom that helps to handle that (like the
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.