all groups > dotnet remoting > july 2003 >
You're in the

dotnet remoting

group:

Size used to serialize ints using BinaryFormatter


Size used to serialize ints using BinaryFormatter hrobinsonAL NO[at]SPAM hotmail.com
7/30/2003 12:36:58 PM
dotnet remoting:
I am new to remoting and looking at what format to use to serialize my
objects. I thought I'd do some tests and I can't understand why I am
getting the results that I am. I am simply serializing a single int
and it seems to take 54 bytes, not 4 as I would expect. If I were
serializing a non-primitive object, I could understand that there's
some header info etc., but a primitive?

Also if I serialize 10 ints, it scales linearly and takes 540 bytes.

Maybe I am using the MemoryStream class wrong?
Thanks, Hugh

Here's the simple code:

try {
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();

int nTest=7;
formatter.Serialize(stream, nTest);

// this prints 54, not 4 as I would expect!
Debug.WriteLine(stream.Length);

// aby.Length is also 54...
byte[] aby = stream.ToArray();
} catch (Exception ex) {
Debug.WriteLine(ex.Message);
Re: Size used to serialize ints using BinaryFormatter Rob Teixeira [MVP]
7/30/2003 6:49:19 PM
There are indeed serialization headers here. For one, the serialization
formatter needs to know what Type the data is (and by default also captures
things like the complete qualified type including version). Otherwise you
have a bunch of bits and no way to determine how to deserialize it.

-Rob [MVP]

[quoted text, click to view]

Size used to serialize ints using BinaryFormatter Stanislaw Szczepaniak
7/31/2003 2:35:23 AM
If you replace the BinaryFormatter by the SoapFormatter
you are able to read what's needed to store (type
information etc.)
I think the overhead doen't matter for larger objects and
does it make sence to serialize a primitive?

And with serialization you have to differnet
possibilities, Xml Serialization and Runtime Serialization.
With the Xml Serialization you do not have to store the
type information and e.g.
public class Person
{
public string Name;
public int Age;
}

with

Person p = new Person();
p.Name = "Tom";
p.Age = 17;

would be serialized simply as

<Person>
<Name>Tom</Name>
<Age>17</age>
</Person>

using Xml Serialization.

Regards,
Stan
Re: Size used to serialize ints using BinaryFormatter hrobinsonAL NO[at]SPAM hotmail.com
7/31/2003 9:07:25 AM
Thanks for the posts.

The reason I was looking at serializing primitive ints is that I was
thinking of writing my own serialization routines for each class that
I want to serialize and I was curious as to how much space data really
takes on the wire. I was thinking that if I implemented my own
serialization then I only need 4 bytes for an int, because I know the
format of the data on the wire (I realize that doing that could lead
to some versioning issues).

I hadn't got as far as looking at SerializationInfo and ISerializable
- I suppose I should look at those next - actually, I think I was
thinking about it from a Java point of view of implementing
serialization (where there isn't a serializationinfo object). Clearly
I need to do more research

AddThis Social Bookmark Button