Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ way to serialize a message?

In my current project I have a few different interfaces that require me to serialize messages into byte buffers. I feel like I'm probably not doing it in a way that would make a true C++ programmer happy (and I'd like to).

I would typically do something like this:

struct MyStruct {
    uint32_t x;
    uint64_t y;
    uint8_t  z[80];
};

uint8_t* serialize(const MyStruct& s) {
    uint8_t* buffer = new uint8_t[sizeof(s)];
    uint8_t* temp = buffer;
    memcpy(temp, &s.x, sizeof(s.x));
    temp += sizeof(s.x);

    //would also have put in network byte order...
    ... etc ...

    return buffer;
}

Excuse any typos, that was just an example off the top of my head. Obviously it can get more complex if the structure I'm serializing has internal pointers.

So, I have two questions that are closely related:

  1. Is there any problem in the specific scenario above with serializing by casting the struct directly to a char buffer assuming I know that the destination systems are in the same endianness?

  2. Main question: Is there a better... erm... C++? way to do this aside from the addition of smart pointers? I feel like this is such a common problem that the STL probably handles it - and if it doesn't, I'm sure there's a better way to do it anyway using C++ mechanisms.

EDIT Bonus points if you can do a clean example of serializing this structure in a better way using standard C++/STL without added libraries.

like image 363
John Humphreys Avatar asked Dec 28 '25 15:12

John Humphreys


1 Answers

You might want to take a look at Google Protocol Buffers (also known as protobuf). You define your data in a language neutral IDL and then run it through a generator to generate your C++ classes. It'll take care of byte ordering issues and can provide a very compact binary form.

By using this you'll not only be able to save your C++ data but it'll be useable in other languages (C#, Java, Python etc) as there are protobuf implementation available for them.

like image 137
Sean Avatar answered Dec 31 '25 05:12

Sean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!