Skip to content

Commit 935b156

Browse files
author
pfeatherstone
committed
updates to readme
1 parent 96fbe62 commit 935b156

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![Windows](https://github.com/pfeatherstone/msgpack_cpp/actions/workflows/windows.yml/badge.svg)
44

55
# 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`.
77

88
## Examples
99

@@ -49,7 +49,9 @@ int main()
4949

5050
### Custom types
5151

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.
5355

5456
```cpp
5557
#include "msgpack.h"
@@ -101,7 +103,12 @@ int main()
101103
}
102104
```
103105
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+
105112
106113
```cpp
107114
#include <boost/describe/class.hpp>
@@ -135,11 +142,13 @@ int main()
135142
// Serialize
136143
std::stringstream buf;
137144
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
139147
140148
// Deserialize
141149
auto in = source(buf);
142-
deserialize(in, b);
150+
deserialize(in, b, /*as_map=*/false);
151+
deserialize(in, b, /*as_map=*/true);
143152
}
144153
```
145154

@@ -206,6 +215,32 @@ Just copy the contents of the include folder in your project with `msgpack_descr
206215
207216
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.
208217
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+
209244
## Documentation
210245

211246
There is none. Hopefully the code is readable.

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ FetchContent_MakeAvailable(Boost)
2121
function(add_example target_name)
2222
add_executable(${target_name} ${ARGN})
2323
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../include)
24+
target_compile_features(${target_name} PRIVATE cxx_std_17)
2425
target_compile_options(${target_name} PRIVATE $<${IS_NOT_MSVC}:-Wall -Wextra -Werror>)
2526
target_link_options(${target_name} PRIVATE $<$<AND:$<CONFIG:RELEASE>,${IS_NOT_MSVC}>:-s>)
2627
target_link_libraries(${target_name} PRIVATE Boost::describe)

0 commit comments

Comments
 (0)