473,378 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1239
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.com

"Jesper, Denmark" <Je***********@discussions.microsoft.comwrote in message
news:FF**********************************@microsof t.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.comwrote:
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*********@nnowslpianmk.comwrote 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.comwrote:
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.microsoft.comwrote in message
news:FF**********************************@microsof t.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.netwrote:
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 SaveDataVersion1
{
// Serializable class representing the original version of the
data structure
}

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

class InternalData
{
public InternalData(SaveDataVersion1 initData)
{
// Initialize based on SaveDataVersion1
}

public InternalData(SaveDataVersion2 initData)
{
// Initialize based on SaveDataVersion2
}
}
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*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Wed, 23 May 2007 12:06:41 -0700, sloan <sl***@ipass.netwrote:
>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 SaveDataVersion1
{
// Serializable class representing the original version of the
data structure
}

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

class InternalData
{
public InternalData(SaveDataVersion1 initData)
{
// Initialize based on SaveDataVersion1
}

public InternalData(SaveDataVersion2 initData)
{
// Initialize based on SaveDataVersion2
}
}
>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*********@nnowslpianmk.comwrote 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(SerializationInfo info, StreamingContext context)
: base (info, context)
{
m_WidgetID = info.GetInt32("WidgetID");
m_WidgetName = info.GetString("WidgetName");

//
// Properties added in V2
//
try
{
m_SpecificValues = info.GetString("SpecificValues");
m_Application = info.GetString("Application");
}
catch (SerializationException)
{
//
// Serialization exception, we must be deserializing a V1
object
//
m_SpecificValues = string.Empty;
m_Application = string.Empty;
}
}

/// <summary>
/// GetObjectData which serializes this Method.
/// </summary>
[SecurityPermission(SecurityAction.Demand,
SerializationFormatter=true)]
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info, context);

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

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

"John Vottero" wrote:
"Jesper, Denmark" <Je***********@discussions.microsoft.comwrote in message
news:FF**********************************@microsof t.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
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...
6
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...
2
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...
3
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
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...
2
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...
2
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...
0
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...
5
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:...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.