|
3 | 3 |  |
4 | 4 |
|
5 | 5 | # msgpack_cpp |
6 | | -C++ header-only msgpack library that supports (de)serializing types directly, including custom types, as well as a dicitionary type `msgpackcpp::value` a la `nlohmann::json` or `boost::json::value`. |
| 6 | +C++ header-only msgpack library that supports (de)serializing types directly, including custom types, as well as a dicitionary type `msgpackcpp::value` à la `nlohmann::json` or `boost::json::value`. |
7 | 7 |
|
8 | 8 | ## Examples |
9 | 9 |
|
@@ -49,7 +49,9 @@ int main() |
49 | 49 |
|
50 | 50 | ### Custom types |
51 | 51 |
|
52 | | -Option 1 : define `serialize()` and `deserialize()` functions in the same namespace as your custom struct |
| 52 | +Option 1 : define `serialize()` and `deserialize()` functions in the same namespace as your custom struct. |
| 53 | +Note the use of std::tie. std::tuple is serialized like a msgpack [array](https://github.com/msgpack/msgpack/blob/master/spec.md#array-format-family). |
| 54 | +This ensures your custom type is serialized into a single msgpack object. |
53 | 55 |
|
54 | 56 | ```cpp |
55 | 57 | #include "msgpack.h" |
@@ -101,7 +103,12 @@ int main() |
101 | 103 | } |
102 | 104 | ``` |
103 | 105 |
|
104 | | -Option 2 : use Boost.Describe to describe your struct |
| 106 | +Option 2 : use Boost.Describe to describe your struct. This automatically makes the following functions available: |
| 107 | +* `serialize(Sink& out, const CustomType& obj, bool as_map = false)` |
| 108 | +* `deserialize(Source& in, CustomType& obj, bool as_map = false)` |
| 109 | +
|
| 110 | +When `as_map == false` then you get exactly the same behaviour as above. When `as_map == true` your type is serialized like a msgpack [map](https://github.com/msgpack/msgpack/blob/master/spec.md#map-format-family) where the keys are the member variable names of your struct. |
| 111 | +
|
105 | 112 |
|
106 | 113 | ```cpp |
107 | 114 | #include <boost/describe/class.hpp> |
@@ -135,11 +142,13 @@ int main() |
135 | 142 | // Serialize |
136 | 143 | std::stringstream buf; |
137 | 144 | auto out = sink(buf); |
138 | | - serialize(out, a); |
| 145 | + serialize(out, a, /*as_map=*/false); // serialize to a msgpack array |
| 146 | + serialize(out, a, /*as_map=*/true); // serialize to a msgpack map |
139 | 147 |
|
140 | 148 | // Deserialize |
141 | 149 | auto in = source(buf); |
142 | | - deserialize(in, b); |
| 150 | + deserialize(in, b, /*as_map=*/false); |
| 151 | + deserialize(in, b, /*as_map=*/true); |
143 | 152 | } |
144 | 153 | ``` |
145 | 154 |
|
@@ -206,6 +215,32 @@ Just copy the contents of the include folder in your project with `msgpack_descr |
206 | 215 |
|
207 | 216 | You just need a C++17 compiler. If you want to avail yourself of the convenient Boost.Describe integration in `msgpack_describe.h`, then you'll require that Boost library. |
208 | 217 |
|
| 218 | +## API |
| 219 | +
|
| 220 | +The library provides the following functions for basic and STL types: |
| 221 | +
|
| 222 | +```cpp |
| 223 | +namespace msgpackcpp |
| 224 | +{ |
| 225 | + template<SINK_TYPE Sink, class Type> |
| 226 | + void serialize(Sink& out, const Type& obj); |
| 227 | +
|
| 228 | + template<SOURCE_TYPE Source, class Type> |
| 229 | + void deserialize(Source& in, Type& obj); |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +where: |
| 234 | +* `out` is a templated function object with signature `void(const char* data, size_t len)` which writes serialized data. |
| 235 | +* `in` is a templated function object with signature `void(char* data, size_t len)` which reads serialized data. |
| 236 | + |
| 237 | +When c++20 is enabled `SINK_TYPE` and `SOURCE_TYPE` are concepts which check those signatures. |
| 238 | + |
| 239 | +The library also provides functions `sink()` and `source()` which take a type and return function objects (c++ lambda) which satisfy the `SINK_TYPE` and `SOURCE_TYPE` concepts. Currently overloads for `std::vector<char>` and `std::iostream` are provided though users can write their own sink/source types. |
| 240 | + |
| 241 | +This library also provides a dictionary type `msgpackcpp::value` very similar to [nlohmann::json](https://json.nlohmann.me/api/basic_json/) or `boost::json::value` which can be (de)serialized, this type using member functions `.pack()` and `.unpack()`. |
| 242 | +Conversions from `msgpackcpp::value` to and from custom types is not supported and discouraged. This library allows you to serialize and deserialized types directly without having to go through `msgpackkcpp::value`. |
| 243 | + |
209 | 244 | ## Documentation |
210 | 245 |
|
211 | 246 | There is none. Hopefully the code is readable. |
0 commit comments