473,770 Members | 5,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialization vs. program updates

Hi,

Just before I invent my own solution... :-)

I'm developing a program where I use binary serialization of the data the
program holds. Know and then the serialized objecst will be modified, e.g.
with a new field, or perhaps the removal of a field. Depending on the
changes, the program will not be able to deserialize the stored data. Is
there any technique I can read about somewhere dealing with this issue.

Regards Jesper
May 23 '07 #1
9 1257
Jesper,

Yes, you can implement the ISerializable interface on the type, and then
handle serialization yourself. You would be able to check for the existence
of fields, and set values for fields that weren't persisted in the
ISerializable interface implementation.

For more information on custom serialization, you can check out the
section of the MSDN documentation titled "Custom Serialization", located at:

http://msdn2.microsoft.com/en-us/library/ty01x675.aspx

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

"Jesper, Denmark" <Je***********@ discussions.mic rosoft.comwrote in message
news:FF******** *************** ***********@mic rosoft.com...
Hi,

Just before I invent my own solution... :-)

I'm developing a program where I use binary serialization of the data the
program holds. Know and then the serialized objecst will be modified, e.g.
with a new field, or perhaps the removal of a field. Depending on the
changes, the program will not be able to deserialize the stored data. Is
there any technique I can read about somewhere dealing with this issue.

Regards Jesper

May 23 '07 #2
On Wed, 23 May 2007 10:33:45 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard .caspershouse.c omwrote:
Yes, you can implement the ISerializable interface on the type, and
then
handle serialization yourself. You would be able to check for the
existence
of fields, and set values for fields that weren't persisted in the
ISerializable interface implementation.
Okay, so I admit I'm a complete novice at serialization, so my apologies
if this is a dumb question, but...

My first approach to a situation like this would be to try to introduce
some sort of versioning logic. Specifically, keep the main data object
class and the serializing objects separate. The main data object would
include constructors to take any version of a serializing object I've
made, and handle missing or new fields appropriately. I'd always
serialize using the newest version of the serializing object (or if you
like, the main data object class could always be the "newest version",
though I expect you'd run into class naming issues doing that, requiring
you to rename the class each time it changed?), and rely on the
deserializing process to instantiate the relevant version which could then
be used to construct the main data object.

My reasoning being that I'm "scared" of having to implement custom
serialization. :) That is, I'm sure I could learn it if I had to, but it
seems like a lot of trouble to go to if I can solve the problem from a
higher-level point of view.

So, what's wrong with that idea? :)

Pete
May 23 '07 #3

You gotta pick one or the other:
If you want backwards compatability, you need to implement ISerializable and
handle the changed/new stuff yourself.
If you want easy, you have to keep the same object "contract".


"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Wed, 23 May 2007 10:33:45 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard .caspershouse.c omwrote:
Yes, you can implement the ISerializable interface on the type, and
then
handle serialization yourself. You would be able to check for the
existence
of fields, and set values for fields that weren't persisted in the
ISerializable interface implementation.

Okay, so I admit I'm a complete novice at serialization, so my apologies
if this is a dumb question, but...

My first approach to a situation like this would be to try to introduce
some sort of versioning logic. Specifically, keep the main data object
class and the serializing objects separate. The main data object would
include constructors to take any version of a serializing object I've
made, and handle missing or new fields appropriately. I'd always
serialize using the newest version of the serializing object (or if you
like, the main data object class could always be the "newest version",
though I expect you'd run into class naming issues doing that, requiring
you to rename the class each time it changed?), and rely on the
deserializing process to instantiate the relevant version which could then
be used to construct the main data object.

My reasoning being that I'm "scared" of having to implement custom
serialization. :) That is, I'm sure I could learn it if I had to, but it
seems like a lot of trouble to go to if I can solve the problem from a
higher-level point of view.

So, what's wrong with that idea? :)

Pete

May 23 '07 #4
"Jesper, Denmark" <Je***********@ discussions.mic rosoft.comwrote in message
news:FF******** *************** ***********@mic rosoft.com...
Hi,

Just before I invent my own solution... :-)

I'm developing a program where I use binary serialization of the data the
program holds. Know and then the serialized objecst will be modified, e.g.
with a new field, or perhaps the removal of a field. Depending on the
changes, the program will not be able to deserialize the stored data. Is
there any technique I can read about somewhere dealing with this issue.
Search for "Version Tolerant Serialization" and you should find this:

http://msdn2.microsoft.com/en-us/library/ms229752.aspx

May 23 '07 #5
On Wed, 23 May 2007 12:06:41 -0700, sloan <sl***@ipass.ne twrote:
You gotta pick one or the other:

If you want backwards compatability, you need to implement ISerializable
and handle the changed/new stuff yourself.
Why? What is it about my proposal that doesn't work?

For example:

class SaveDataVersion 1
{
// Serializable class representing the original version of the
data structure
}

class SaveDataVersion 2
{
// Serializable class representing the second version of the data
structure
}

class InternalData
{
public InternalData(Sa veDataVersion1 initData)
{
// Initialize based on SaveDataVersion 1
}

public InternalData(Sa veDataVersion2 initData)
{
// Initialize based on SaveDataVersion 2
}
}
If you want easy, you have to keep the same object "contract".
The above code seems easy enough to me, and yet allows for multiple
versions of the serializable data.

Pete
May 23 '07 #6
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Wed, 23 May 2007 12:06:41 -0700, sloan <sl***@ipass.ne twrote:
>You gotta pick one or the other:

If you want backwards compatability, you need to implement ISerializable
and handle the changed/new stuff yourself.

Why? What is it about my proposal that doesn't work?

For example:

class SaveDataVersion 1
{
// Serializable class representing the original version of the
data structure
}

class SaveDataVersion 2
{
// Serializable class representing the second version of the data
structure
}

class InternalData
{
public InternalData(Sa veDataVersion1 initData)
{
// Initialize based on SaveDataVersion 1
}

public InternalData(Sa veDataVersion2 initData)
{
// Initialize based on SaveDataVersion 2
}
}
>If you want easy, you have to keep the same object "contract".

The above code seems easy enough to me, and yet allows for multiple
versions of the serializable data.
There's nothing wrong with your approach but, implementing ISerializable
would be a lot easier. V2.0 of the .NET Framework added support for version
tolerant serialization which makes things even easier.

May 23 '07 #7
On Wed, 23 May 2007 15:05:41 -0700, John Vottero <JV******@mvpsi .com>
wrote:
There's nothing wrong with your approach but, implementing ISerializable
would be a lot easier.
If you say so. I'm not sure what could be easier than specifying some
fields, and writing a tiny amount of code that is essentially a sequence
of assignments. But as I said, I don't know what goes into writing a
custom serializing class, so maybe it in fact is even easier than that.
V2.0 of the .NET Framework added support for version tolerant
serialization which makes things even easier.
I had no idea until the link was posted...I intend to read through that
and see if it makes sense to me. Certainly if .NET actually handles
versioning for serialized data, that seems like the way to go.

Pete
May 23 '07 #8
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Wed, 23 May 2007 15:05:41 -0700, John Vottero <JV******@mvpsi .com>
wrote:
>There's nothing wrong with your approach but, implementing ISerializable
would be a lot easier.

If you say so. I'm not sure what could be easier than specifying some
fields, and writing a tiny amount of code that is essentially a sequence
of assignments. But as I said, I don't know what goes into writing a
custom serializing class, so maybe it in fact is even easier than that.
Specifying some fields and writing a sequence of assigments is basically
what's involved in implementing ISerializable. There's really nothing to it
although, version tolerant serialization mostly eliminates the need to do
it.

Here's an example of implementing ISerializable for the Widget class:

#region Implementation of ISerializable

/// <summary>
/// Deserialization constructor.
/// </summary>
private Widget(Serializ ationInfo info, StreamingContex t context)
: base (info, context)
{
m_WidgetID = info.GetInt32(" WidgetID");
m_WidgetName = info.GetString( "WidgetName ");

//
// Properties added in V2
//
try
{
m_SpecificValue s = info.GetString( "SpecificValues ");
m_Application = info.GetString( "Applicatio n");
}
catch (SerializationE xception)
{
//
// Serialization exception, we must be deserializing a V1
object
//
m_SpecificValue s = string.Empty;
m_Application = string.Empty;
}
}

/// <summary>
/// GetObjectData which serializes this Method.
/// </summary>
[SecurityPermiss ion(SecurityAct ion.Demand,
SerializationFo rmatter=true)]
public override void GetObjectData(S erializationInf o info,
StreamingContex t context)
{
base.GetObjectD ata(info, context);

info.AddValue(" MethodID", m_MethodID);
info.AddValue(" MethodName", m_MethodName);

info.AddValue(" SpecificValues" , m_SpecificValue s);
info.AddValue(" Application", m_Application);
}
#endregion
May 24 '07 #9
Thanks a lot, all of you!.

"John Vottero" wrote:
"Jesper, Denmark" <Je***********@ discussions.mic rosoft.comwrote in message
news:FF******** *************** ***********@mic rosoft.com...
Hi,

Just before I invent my own solution... :-)

I'm developing a program where I use binary serialization of the data the
program holds. Know and then the serialized objecst will be modified, e.g.
with a new field, or perhaps the removal of a field. Depending on the
changes, the program will not be able to deserialize the stored data. Is
there any technique I can read about somewhere dealing with this issue.

Search for "Version Tolerant Serialization" and you should find this:

http://msdn2.microsoft.com/en-us/library/ms229752.aspx
May 24 '07 #10

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

Similar topics

0
1620
by: HakonB | last post by:
Hi all I get an exception when trying to deserialize a simple configuration file using XML Serialization. The very long stacktrace can be seen at the bottom of this message. I've see other messages related a similar error but none of the solutions suggested are valid in case :/ I have tracked the problem down to the compilation of the temporary DLL that handles the actual serialization. The following commandline (that is one single...
6
2717
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or rmi. Just to keep my question short I am posting trimmed version of my code. //file: Serializable.cs
2
2541
by: Luck | last post by:
Hi, I really need some expert help... please! Basically, I need to serialize a data structure object to a file using SOAP and then load and de-serialize that file in ANOTHER program. When I serialize / deserialize the object in the SAME application, it works fine but when I simply copy the identical code to another application and try deserialization from that second application, I get the error: An unhandled exception of type...
3
15102
by: Paulo Morgado [MVP] | last post by:
Hi all ..NET Framework 1.1 I have created several types that are serailized to XML as strings. Someting like this: public struct MyInt32 : IXmlSerializable { int value;
5
6030
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the assembly 3- I am trying to deserialize from an EXE that references version 2.0 of the assembly 4- Both version 1.0 and 2.0 of the assembly reside in the GAC (no policy redirects exist).
2
1630
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for this. So I wrote the following code to serialize\deserialize the object, but now I have the problem that the object has a generic list and when I'm trying to deserialize it, I get an error. <System.Serializable()Public Class TestClass
2
1778
by: =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno | last post by:
I am developing a Windows Service that is resident on the machine. The program needs to synchronize certain object list in memory (an object typed as List<Foo>) with disc, serializing and deserializing XML. The first simplest technique I have used ATM is the following: - At program start, if the XML file exists, deserialize it and create an object with its content. - Every time the list changes (an element is added or remove), I...
0
1203
by: nobin01 | last post by:
Dear sir; I want ur Help in serialization.I know serialization.I Know binary,soap and xmlserialization also.But i want ur help in following topics.pls help me as soon as possible.I have search in site but only basics serialization is there. Topics: 1) serialize a nested object in mutiple files by maintaining the relation on parent level. ie, let say object1 containts object2 and object2 contains object3 , while serializing the object1,...
5
6118
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException: Value -8590321990885400808 is outside the valid range . Parameter name: ticks at System.DateTime..ctor (Int64 ticks) at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadPrimitiv
0
9425
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
10225
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...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
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
8880
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
6676
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();...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
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
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.