Custom Serialization

G

Guest

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

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 said:
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]
- (e-mail address removed)

Val said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)


Val said:
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 said:
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]
- (e-mail address removed)

Val said:
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
 
T

Thomas T. Veldhouse

Val said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top