S
S
Sersoftin2018-06-19 21:53:01
Computer networks
Sersoftin, 2018-06-19 21:53:01

How to serialize data to binary format?

There is an object. The object contains all sorts of data, from boolean variables to strings. How to serialize this whole economy into an object without an extra overhead? And to change the byte order to big endian.
Here I have a structure:

struct Point {
    int x;
    int y;
    int z;
    std::string name;
};

// заполним ее
Point point;
point.x = 5;
point.y = 10;
point.z = 7;
point.name = "test";

Now I want to do something like:
std::vector<byte> buff = serializer::to_binary(point);

And in the end I want this:
// мы сериализовали данные в бинарный формат
0x00 0x00 0x00 0x05 0x00 0x00 0x00 0x0A 0x00 0x00 0x00 0x07 0x74 0x65 0x73 0x74 0x00

In what ways can this be achieved? It is clear that there is hardly a solution that can do everything right at once the way I want. But suddenly someone knows a bunch of something to achieve such an effect. Do not offer any Boost.Serialization. Don't suggest changing the format either. Write by hand, convert the byte order with all sorts of htons, htonl, etc. - no thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2018-06-19
@Taraflex

Do not offer any Boost.Serialization.

Until compile time reflection is brought into c++, then only for POD types https://habr.com/post/344206/
Just take protobuf and don't suffer.

A
Alexey Sergey, 2018-06-20
@dev_random

I somehow had a chance to try this: https://github.com/USCiLab/cereal
It started up easily, stl containers work out of the box, it's easy to extend serialization to your structures and classes.
I remember exactly that it was possible to write in cross-platform-binary and json formats.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question