Skip to content

Commit 96fbe62

Browse files
author
me
committed
readme and example
1 parent 3ae3ce9 commit 96fbe62

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

README.md

Lines changed: 56 additions & 1 deletion
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
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`.
77

88
## Examples
99

@@ -143,6 +143,61 @@ int main()
143143
}
144144
```
145145

146+
### Dictionary value
147+
148+
```cpp
149+
#include "msgpack.h"
150+
#include "msgpack_sinks.h"
151+
152+
const auto REQUIRE = [](auto res) {if (!res)throw std::runtime_error("bad");};
153+
154+
int main()
155+
{
156+
using msgpackcpp::serialize;
157+
using msgpackcpp::deserialize;
158+
using msgpackcpp::sink;
159+
using msgpackcpp::source;
160+
161+
// Data
162+
msgpackcpp::value jv0 = {
163+
{"pi", 3.141},
164+
{"happy", true},
165+
{"name", "Niels"},
166+
{"nothing", nullptr},
167+
{"answer", {
168+
{"everything", -42}
169+
}},
170+
{"list", {1, 0, 2}},
171+
{"object", {
172+
{"currency", "USD"},
173+
{"value", 42.99}
174+
}}
175+
};
176+
177+
// Serialize (pack)
178+
std::vector<char> buf;
179+
auto out = sink(buf);
180+
jv0.pack(out);
181+
182+
// Deserialize (unpack)
183+
msgpackcpp::value jv1;
184+
auto in = source(buf);
185+
jv1.unpack(in);
186+
187+
// Check
188+
REQUIRE(jv1.is_object());
189+
REQUIRE(jv1.size() == 7);
190+
REQUIRE(jv1.at("pi").as_real() == 3.141);
191+
REQUIRE(jv1.at("happy").as_bool() == true);
192+
REQUIRE(jv1.at("name").as_str() == "Niels");
193+
REQUIRE(jv1.at("nothing").is_null());
194+
REQUIRE(jv1.at("answer").at("everything").as_int64() == -42);
195+
REQUIRE(jv1.at("list").as_array().size() == 3);
196+
REQUIRE(jv1.at("object").at("currency").as_str() == "USD");
197+
REQUIRE(jv1.at("object").at("value").as_real() == 42.99);
198+
}
199+
```
200+
146201
## Installation
147202
148203
Just copy the contents of the include folder in your project with `msgpack_describe.h` as an optional header.

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ endfunction()
2828

2929
add_example(example1 example1.cpp)
3030
add_example(example2 example2.cpp)
31-
add_example(example3 example3.cpp)
31+
add_example(example3 example3.cpp)
32+
add_example(example4 example4.cpp)

examples/example4.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "msgpack.h"
2+
#include "msgpack_sinks.h"
3+
4+
const auto REQUIRE = [](auto res) {if (!res)throw std::runtime_error("bad");};
5+
6+
int main()
7+
{
8+
using msgpackcpp::serialize;
9+
using msgpackcpp::deserialize;
10+
using msgpackcpp::sink;
11+
using msgpackcpp::source;
12+
13+
// Data
14+
msgpackcpp::value jv0 = {
15+
{"pi", 3.141},
16+
{"happy", true},
17+
{"name", "Niels"},
18+
{"nothing", nullptr},
19+
{"answer", {
20+
{"everything", -42}
21+
}},
22+
{"list", {1, 0, 2}},
23+
{"object", {
24+
{"currency", "USD"},
25+
{"value", 42.99}
26+
}}
27+
};
28+
29+
// Serialize (pack)
30+
std::vector<char> buf;
31+
auto out = sink(buf);
32+
jv0.pack(out);
33+
34+
// Deserialize (unpack)
35+
msgpackcpp::value jv1;
36+
auto in = source(buf);
37+
jv1.unpack(in);
38+
39+
// Check
40+
REQUIRE(jv1.is_object());
41+
REQUIRE(jv1.size() == 7);
42+
REQUIRE(jv1.at("pi").as_real() == 3.141);
43+
REQUIRE(jv1.at("happy").as_bool() == true);
44+
REQUIRE(jv1.at("name").as_str() == "Niels");
45+
REQUIRE(jv1.at("nothing").is_null());
46+
REQUIRE(jv1.at("answer").at("everything").as_int64() == -42);
47+
REQUIRE(jv1.at("list").as_array().size() == 3);
48+
REQUIRE(jv1.at("object").at("currency").as_str() == "USD");
49+
REQUIRE(jv1.at("object").at("value").as_real() == 42.99);
50+
}

0 commit comments

Comments
 (0)