By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,896 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,896 IT Pros & Developers. It's quick & easy.

Custom Serialization

P: n/a
Val
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)serializing(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

Sep 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"implementing a TypeConverter":

http://www.codeguru.com/columns/vb/article.php/c6529/

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Val" <Va*@discussions.microsoft.comwrote in message
news:A9**********************************@microsof t.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)serializing(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

Sep 21 '06 #2

P: n/a
Val
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
"implementing a TypeConverter":

http://www.codeguru.com/columns/vb/article.php/c6529/

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Val" <Va*@discussions.microsoft.comwrote in message
news:A9**********************************@microsof t.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)serializing(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


Sep 21 '06 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com
"Val" <Va*@discussions.microsoft.comwrote in message
news:CB**********************************@microsof t.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
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
"implementing a TypeConverter":

http://www.codeguru.com/columns/vb/article.php/c6529/

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Val" <Va*@discussions.microsoft.comwrote in message
news:A9**********************************@microso ft.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)serializing(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



Sep 21 '06 #4

P: n/a
Val <Va*@discussions.microsoft.comwrote:
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 TestSerializableClass : ISerializable
{
private int i = 0;
private int j = 0;

public TestSerializableClass()
{

}

public TestSerializableClass(SerializationInfo info,
StreamingContext context)
{
string serialString = info.GetString(@"DATA");

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

#region ISerializable Members

public void GetObjectData(SerializationInfo info,
StreamingContext 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
Sep 21 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.