|
| 1 | +# Boost.Serialization |
| 2 | + |
| 3 | +For Boost.Serialization support, you must also include the header `<rfl/boost_serialization.hpp>` and link to the [Boost.Serialization](https://www.boost.org/doc/libs/release/libs/serialization/) library. |
| 4 | +Furthermore, when compiling reflect-cpp, you need to pass `-DREFLECTCPP_BOOST_SERIALIZATION=ON` to cmake. If you are using vcpkg, there |
| 5 | +should be an appropriate feature that will abstract this away for you. |
| 6 | + |
| 7 | +Unlike most other formats supported by reflect-cpp, which use document-tree based libraries, Boost.Serialization streams data sequentially through archive objects. reflect-cpp implements this as a schemaful binary format, similar to Avro and Cap'n Proto. |
| 8 | + |
| 9 | +## Reading and writing |
| 10 | + |
| 11 | +Suppose you have a struct like this: |
| 12 | + |
| 13 | +```cpp |
| 14 | +struct Person { |
| 15 | + std::string first_name; |
| 16 | + std::string last_name; |
| 17 | + rfl::Timestamp<"%Y-%m-%d"> birthday; |
| 18 | + std::vector<Person> children; |
| 19 | +}; |
| 20 | +``` |
| 21 | +
|
| 22 | +A `Person` can be serialized like this: |
| 23 | +
|
| 24 | +```cpp |
| 25 | +const auto person = Person{...}; |
| 26 | +const std::vector<char> bytes = rfl::boost_serialization::write(person); |
| 27 | +``` |
| 28 | + |
| 29 | +You can parse bytes like this: |
| 30 | + |
| 31 | +```cpp |
| 32 | +const rfl::Result<Person> result = rfl::boost_serialization::read<Person>(bytes); |
| 33 | +``` |
| 34 | + |
| 35 | +## Loading and saving |
| 36 | + |
| 37 | +You can also load and save to disc using a very similar syntax: |
| 38 | + |
| 39 | +```cpp |
| 40 | +const rfl::Result<Person> result = rfl::boost_serialization::load<Person>("/path/to/file.bin"); |
| 41 | + |
| 42 | +const auto person = Person{...}; |
| 43 | +rfl::boost_serialization::save("/path/to/file.bin", person); |
| 44 | +``` |
| 45 | +
|
| 46 | +## Reading from and writing into streams |
| 47 | +
|
| 48 | +You can also read from and write into any `std::istream` and `std::ostream` respectively. |
| 49 | +
|
| 50 | +```cpp |
| 51 | +const rfl::Result<Person> result = rfl::boost_serialization::read<Person>(my_istream); |
| 52 | +
|
| 53 | +const auto person = Person{...}; |
| 54 | +rfl::boost_serialization::write(person, my_ostream); |
| 55 | +``` |
| 56 | + |
| 57 | +## Archive interop |
| 58 | + |
| 59 | +One of the key features of the Boost.Serialization backend is that you can use it with existing Boost archives directly. This allows reflect-cpp types to participate in larger Boost serialization workflows. |
| 60 | + |
| 61 | +```cpp |
| 62 | +#include <boost/archive/binary_oarchive.hpp> |
| 63 | +#include <boost/archive/binary_iarchive.hpp> |
| 64 | +#include <rfl/boost_serialization.hpp> |
| 65 | + |
| 66 | +// Writing into an existing Boost archive |
| 67 | +boost::archive::binary_oarchive oa(my_ostream); |
| 68 | +rfl::boost_serialization::write(oa, person); |
| 69 | + |
| 70 | +// Reading from an existing Boost archive |
| 71 | +boost::archive::binary_iarchive ia(my_istream); |
| 72 | +const auto result = rfl::boost_serialization::read_from_archive< |
| 73 | + Person, boost::archive::binary_iarchive, |
| 74 | + boost::archive::binary_oarchive>(ia); |
| 75 | +``` |
| 76 | +
|
| 77 | +This also works with text archives: |
| 78 | +
|
| 79 | +```cpp |
| 80 | +#include <boost/archive/text_oarchive.hpp> |
| 81 | +#include <boost/archive/text_iarchive.hpp> |
| 82 | +
|
| 83 | +boost::archive::text_oarchive oa(my_ostream); |
| 84 | +rfl::boost_serialization::write(oa, person); |
| 85 | +
|
| 86 | +boost::archive::text_iarchive ia(my_istream); |
| 87 | +const auto result = rfl::boost_serialization::read_from_archive< |
| 88 | + Person, boost::archive::text_iarchive, |
| 89 | + boost::archive::text_oarchive>(ia); |
| 90 | +``` |
0 commit comments