views:

385

answers:

2

I know that you can use boost serialization to serialize to a text format and then push over a socket, but I'd like to serialize a class of statistics data into a binary format (both for size and encoding/decoding overhead reasons). Is it safe to use boost serialization for this?

My specific worries are:

  1. Differences between integer type sizes on different platforms (mainly 32-bit vs 64-bit).
    Though I can largely get around this by using exactly-sized integer from stdint, I'd still like to understand the behavior.
  2. Differences in endianness between systems, does boost serialize into a standard endian-ness (eg: network ordering), and then deserialize using the host's endianness?

It's a very nice library, but unfortunately documentation on it's binary capabilities is somewhat limited, so I just want to make sure that using it this way would be safe.

A: 

No, in general boost binary serialization is not machine-independent. See here.

rlbond
The only thing I see relating to portability is this statement of one of their goals:"Data Portability - Streams of bytes created on one platform should be readable on any other."
gct
Ah. Silly frames. Check the updated link. Scroll down about 1/3.
rlbond
Fraid I'm still not seeing it, can you quote a string I can ctrl-f on?
gct
He might be referring to the first code block in the "Archive Models" section, where a comment describes the binary archive type as being non-portable.
Josh Townzen
Aha! subtle, I'll assume it's not safe to use it then. Fortunately my classes are relatively simple, I'll just roll the serialization by hand.
gct
In general it is portable, specific to binary archives it is not. This answer is quite a misrepresentation.
Hassan Syed
I wrote that boost *binary* serialization is not machine-independent. Please reread my answer.
rlbond
+3  A: 

It's available, I've been hearing a lot about Google's protobuf. It has a C and C++ binding.

Hans Passant
I've never used protobuf and wasn't sure whether it would handle endianness correctly. I did some searching and found this, which states that it will always deserialize to the local machine's native byte order, so it should work for your purposes: http://markmail.org/message/3ewnsvs4dwvwt647?q=thread:3ewnsvs4dwvwt647#query:thread%3A3ewnsvs4dwvwt647+page:1+mid:lv5yjf7qefizc2bv+state:results
Josh Townzen