472,950 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Of Structs, Layouts, and Serialization

I have a class that needs to be serialized to a byte buffer. I'm using
a large, unwieldy, homegrown serialization method, which has to change
every time the class's members change, and I think there's probably a
better way.

I could do the job generically, and in just a few lines, by declaring
the class as [Serializable()], then using a BinaryFormatter to
serialize the object to a memory stream, then getting the buffer from
the memory stream. However, that generates a buffer that's much larger
than the actual size of the members in the class.

Is there an easy way of serializing the object to a byte buffer in a
space-efficient way?

Nov 16 '06 #1
5 1922
When you serialize a class using Serializable in .NET I believe it
breaks your class and it's valuables into XML, which is why it becomes
so large. As far as I know, that is the only utility in .NET to
serialize a class, and if you need something smaller you will have to
create it yourself. I feel your pain though as I have to support a
serialization method for a class that changes.

dv*****@gmail.com wrote:
I have a class that needs to be serialized to a byte buffer. I'm using
a large, unwieldy, homegrown serialization method, which has to change
every time the class's members change, and I think there's probably a
better way.

I could do the job generically, and in just a few lines, by declaring
the class as [Serializable()], then using a BinaryFormatter to
serialize the object to a memory stream, then getting the buffer from
the memory stream. However, that generates a buffer that's much larger
than the actual size of the members in the class.

Is there an easy way of serializing the object to a byte buffer in a
space-efficient way?
Nov 16 '06 #2
justin creasy wrote:
When you serialize a class using Serializable in .NET I believe it
breaks your class and it's valuables into XML, which is why it becomes
so large. As far as I know, that is the only utility in .NET to
serialize a class, and if you need something smaller you will have to
create it yourself. I feel your pain though as I have to support a
serialization method for a class that changes.

dv*****@gmail.com wrote:
>I have a class that needs to be serialized to a byte buffer. I'm using
a large, unwieldy, homegrown serialization method, which has to change
every time the class's members change, and I think there's probably a
better way.

I could do the job generically, and in just a few lines, by declaring
the class as [Serializable()], then using a BinaryFormatter to
serialize the object to a memory stream, then getting the buffer from
the memory stream. However, that generates a buffer that's much larger
than the actual size of the members in the class.

Is there an easy way of serializing the object to a byte buffer in a
space-efficient way?
Hi Justin (and OP),

Not if you use the BinaryFormatter. The BinaryFormatter produces a binary
representation of the serialised class, i.e. in machine-readable form.

The reason that the resulting data is larger than simply the members in the
class is because it contains metadata required to deserialise the class
correctly, specifically versioning and type information.

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 16 '06 #3
Hey Tom, he actually is using the BinaryFormatter and his problem is
the overhead created by it. It's a great tool for rapid development,
but many times that overhead can become a big problem. As far as I
know, outside of the BinaryFormatter, the only way to have a low
overhead serialization function is to create it yourself. Please let me
know if I am missing something.

Tom Spink wrote:
justin creasy wrote:
When you serialize a class using Serializable in .NET I believe it
breaks your class and it's valuables into XML, which is why it becomes
so large. As far as I know, that is the only utility in .NET to
serialize a class, and if you need something smaller you will have to
create it yourself. I feel your pain though as I have to support a
serialization method for a class that changes.

dv*****@gmail.com wrote:
I have a class that needs to be serialized to a byte buffer. I'm using
a large, unwieldy, homegrown serialization method, which has to change
every time the class's members change, and I think there's probably a
better way.

I could do the job generically, and in just a few lines, by declaring
the class as [Serializable()], then using a BinaryFormatter to
serialize the object to a memory stream, then getting the buffer from
the memory stream. However, that generates a buffer that's much larger
than the actual size of the members in the class.

Is there an easy way of serializing the object to a byte buffer in a
space-efficient way?

Hi Justin (and OP),

Not if you use the BinaryFormatter. The BinaryFormatter produces a binary
representation of the serialised class, i.e. in machine-readable form.

The reason that the resulting data is larger than simply the members in the
class is because it contains metadata required to deserialise the class
correctly, specifically versioning and type information.

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 16 '06 #4
Really, it shouldn't be too hard to roll-your-own
unsafe-binaryformatter that has no metadata, as long as you didn't mind
not using polymorphism and kept your classes in an a strict tree.
Horribly inappropriate for lobbing complex objects around, but would be
pretty sweet for passing structs around when you know what type to
expect. Just use reflection to iterate across the members (sort
members into an order though since order is undefined), serialize them
each individually into your stream, and call it done. Recurse where
you find a struct or class. The thing would stack-overflow if it
wasn't a strict tree though, and you'd have to use the default
Constructor() for the expected type, so no polymorphism unless you
roll-your-own metadata... but otherwise it should work.

justin creasy wrote:
Hey Tom, he actually is using the BinaryFormatter and his problem is
the overhead created by it. It's a great tool for rapid development,
but many times that overhead can become a big problem. As far as I
know, outside of the BinaryFormatter, the only way to have a low
overhead serialization function is to create it yourself. Please let me
know if I am missing something.

Tom Spink wrote:
justin creasy wrote:
When you serialize a class using Serializable in .NET I believe it
breaks your class and it's valuables into XML, which is why it becomes
so large. As far as I know, that is the only utility in .NET to
serialize a class, and if you need something smaller you will have to
create it yourself. I feel your pain though as I have to support a
serialization method for a class that changes.
>
dv*****@gmail.com wrote:
>I have a class that needs to be serialized to a byte buffer. I'm using
>a large, unwieldy, homegrown serialization method, which has to change
>every time the class's members change, and I think there's probably a
>better way.
>>
>I could do the job generically, and in just a few lines, by declaring
>the class as [Serializable()], then using a BinaryFormatter to
>serialize the object to a memory stream, then getting the buffer from
>the memory stream. However, that generates a buffer that's much larger
>than the actual size of the members in the class.
>>
>Is there an easy way of serializing the object to a byte buffer in a
>space-efficient way?
Hi Justin (and OP),

Not if you use the BinaryFormatter. The BinaryFormatter produces a binary
representation of the serialised class, i.e. in machine-readable form.

The reason that the resulting data is larger than simply the members in the
class is because it contains metadata required to deserialise the class
correctly, specifically versioning and type information.

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 16 '06 #5
justin creasy wrote:
Hey Tom, he actually is using the BinaryFormatter and his problem is
the overhead created by it. It's a great tool for rapid development,
but many times that overhead can become a big problem. As far as I
know, outside of the BinaryFormatter, the only way to have a low
overhead serialization function is to create it yourself. Please let me
know if I am missing something.

Tom Spink wrote:
>justin creasy wrote:
When you serialize a class using Serializable in .NET I believe it
breaks your class and it's valuables into XML, which is why it becomes
so large. As far as I know, that is the only utility in .NET to
serialize a class, and if you need something smaller you will have to
create it yourself. I feel your pain though as I have to support a
serialization method for a class that changes.

dv*****@gmail.com wrote:
I have a class that needs to be serialized to a byte buffer. I'm
using a large, unwieldy, homegrown serialization method, which has to
change every time the class's members change, and I think there's
probably a better way.

I could do the job generically, and in just a few lines, by declaring
the class as [Serializable()], then using a BinaryFormatter to
serialize the object to a memory stream, then getting the buffer from
the memory stream. However, that generates a buffer that's much
larger than the actual size of the members in the class.

Is there an easy way of serializing the object to a byte buffer in a
space-efficient way?

Hi Justin (and OP),

Not if you use the BinaryFormatter. The BinaryFormatter produces a
binary representation of the serialised class, i.e. in machine-readable
form.

The reason that the resulting data is larger than simply the members in
the class is because it contains metadata required to deserialise the
class correctly, specifically versioning and type information.

--
Hope this helps,
Tom Spink

Google first, ask later.
Justin,

I meant to infer that serialising the class does not necessarily produce
XML. Your post seemed to imply that serialisation produced XML. Using a
BinaryFormatter, no XML is produced.

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 16 '06 #6

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

Similar topics

16
by: Michael Rozdoba | last post by:
I'm far from a CSS expert, but what I see of it I really like & I love keeping content & style separate. I also hate the way table layout produces convoluted bulky code. However when asked why...
1
by: Fuzzy via .NET 247 | last post by:
I have a struct that I would like to control the field layout on in the same manner of UNION structs in C++. This is not for passing data to unmanaged code, it is to save memory space because field...
12
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
15
by: Daniel Rudy | last post by:
What is the difference between packed and unpacked structs? -- Daniel Rudy Email address has been base64 encoded to reduce spam Decode email address using b64decode or uudecode -m Why...
1
by: Chris Ashley | last post by:
I have an array of structs which I am serializing. Is there a tag I can use to change the name of the struct members in the output XML? I know I can change the root node name with: private...
5
by: ravi.sathyam | last post by:
Hey, So say I have a sockaddr_in struct stored in a packet which I receive from my udp socket.....and it is stored within a certain offset into this packet (which is basically a char array)....
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
12
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
0
by: dans | last post by:
I want to serialise an array of structs into a MemoryStream so I can pass the contents in a message. I have written something that will Serialize and Deserialize in individual object. Is there a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.