Skip to content

Commit 4883da3

Browse files
committed
Merge branch 'dev'
2 parents 423fba1 + 315d086 commit 4883da3

File tree

3 files changed

+529
-12
lines changed

3 files changed

+529
-12
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
*
3+
* @file 08_to_nlohmann_json.cpp
4+
* @author Gaspard Kirira
5+
*
6+
* Copyright 2025, Gaspard Kirira.
7+
* All rights reserved.
8+
* https://github.com/vixcpp/vix
9+
*
10+
* Use of this source code is governed by a MIT license
11+
* that can be found in the License file.
12+
*
13+
* Vix.cpp
14+
*/
15+
16+
#include <iostream>
17+
18+
#include <vix/json/Simple.hpp>
19+
#include <vix/json/convert.hpp> // to_json(Simple) -> Json
20+
#include <vix/json/json.hpp> // vix::json::Json
21+
22+
namespace
23+
{
24+
void example_basic_object()
25+
{
26+
using namespace vix::json;
27+
28+
kvs user = obj({
29+
"name",
30+
"Alice",
31+
"age",
32+
30,
33+
"ok",
34+
true,
35+
"score",
36+
12.5,
37+
"skills",
38+
array({"C++", "P2P", "Offline-first"}),
39+
});
40+
41+
Json j = to_json(user);
42+
43+
std::cout << "\n[example_basic_object]\n";
44+
std::cout << j.dump(2) << "\n";
45+
}
46+
47+
void example_nested()
48+
{
49+
using namespace vix::json;
50+
51+
kvs root = obj({
52+
"app",
53+
"Vix.cpp",
54+
"meta",
55+
obj({
56+
"country",
57+
"UG",
58+
"version",
59+
"1.0.0",
60+
"features",
61+
array({"http", "ws", "p2p"}),
62+
}),
63+
"users",
64+
array({
65+
obj({"id", 1, "name", "Ada"}),
66+
obj({"id", 2, "name", "Gaspard"}),
67+
}),
68+
});
69+
70+
token t = root; // token root holding object
71+
72+
Json j = to_json(t);
73+
74+
std::cout << "\n[example_nested]\n";
75+
std::cout << j.dump(2) << "\n";
76+
}
77+
78+
void example_roundtrip_style_usage()
79+
{
80+
using namespace vix::json;
81+
82+
kvs payload = obj({
83+
"type",
84+
"notification",
85+
"data",
86+
obj({
87+
"title",
88+
"Build OK",
89+
"code",
90+
200,
91+
"tags",
92+
array({"ci", "release", "vix"}),
93+
}),
94+
});
95+
96+
Json j = to_json(payload);
97+
98+
std::cout << "\n[example_roundtrip_style_usage]\n";
99+
std::cout << "type=" << j.value("type", "missing") << "\n";
100+
std::cout << j.dump(2) << "\n";
101+
}
102+
103+
} // namespace
104+
105+
int main()
106+
{
107+
example_basic_object();
108+
example_nested();
109+
example_roundtrip_style_usage();
110+
return 0;
111+
}

0 commit comments

Comments
 (0)