-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathjson_nlohmann_demo.cpp
164 lines (128 loc) · 5.23 KB
/
json_nlohmann_demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
GS
June 2022
This is a quick demo to practice using the nlohmann "JSON for Modern C++" header-only library for
parsing (deserializing) and printing (serializing) JSON data. This is particularly useful for
parsing HTTP REST API responses received from command-line `curl` or the C `libcurl`!
STATUS: done and works perfectly! EXCELLENT demo! It show how to print JSON objects using both
C-style `printf()`-based *and* C++-style prints!
To compile and run (assuming you've already `cd`ed into this dir):
[see the "BEST" option below for my final answer for this code]
```bash
# NB: you may need to use `-std=gnu++17` instead of `-std=c++17` in order to obtain extra GNU
# gcc features, including gcc extensions, POSIX cmds, and Linux sytem cmds.
# See: [my answer]: https://stackoverflow.com/a/71801111/4561887
# 1st: FIRST, follow the C++ `nlohmann/json` library setup and installation instructions here:
# "eRCaGuy_hello_world/cpp/README.md"
# 2nd: THEN, run this build & run command:
#
time g++ -Wall -Wextra -Werror -O3 -std=c++17 -I"json/single_include" json_nlohmann_demo.cpp -o bin/a && bin/a
# Or, WITH ccache compiler caching to speed up builds!:
time ccache g++ -Wall -Wextra -Werror -O3 -std=c++17 -I"json/single_include" json_nlohmann_demo.cpp -o bin/a && bin/a
```
References:
1. nlohmann/json:
1. https://github.com/nlohmann/json#serialization--deserialization
1. https://json.nlohmann.me/
1. API documentation: https://json.nlohmann.me/api/basic_json/
1. See `get<>()` examples at the bottom of this page here!:
https://json.nlohmann.me/api/basic_json/get/
1. See `get_ptr<>()` examples at the bottom of
here: https://json.nlohmann.me/api/basic_json/get_ptr/
1. https://json.nlohmann.me/api/basic_json/get_ref/
1. https://en.cppreference.com/w/cpp/language/string_literal
*/
// Local includes
// NA
// 3rd-party library includes
#include <nlohmann/json.hpp> // a header-only library
// Linux includes
// NA
// C and C++ includes
#include <cstdint> // For `uint8_t`, `int8_t`, etc.
#include <cstdio> // For `printf()`
#include <iostream> // For `std::cin`, `std::cout`, `std::endl`, etc.
#include <string>
// int main(int argc, char *argv[]) // alternative prototype
int main()
{
printf("Hello ");
std::cout << "world!\n\n";
// See a bunch of examples here:
// https://github.com/nlohmann/json#serialization--deserialization
// Create a json object from a raw (`R`) C++ string literal.
// NB: the `parse()` method can throw C++ run-time exceptions if parsing fails!
// Ex: change `true` below to `True` and it will throw a run-time exception and core dump since
// that is not a valid literal! Example error:
//
// terminate called after throwing an instance of 'nlohmann::detail::parse_error'
// what(): [json.exception.parse_error.101] parse error at line 4, column 23: syntax error while parsing value - invalid literal; last read: '"status": T'
// Aborted (core dumped)
//
nlohmann::json j1 = nlohmann::json::parse(R"(
{
"name": "John",
"status": true,
"pi": 3.141
}
)");
// C++ prints using `std::cout`
std::cout << "name: " << j1["name"] << "\n";
std::cout << "status: " << j1["status"] << "\n";
std::cout << "pi: " << j1["pi"] << "\n";
std::cout << "\n";
// C-style prints using `printf()`
//
// See also the examples at the bottom of the documentation for `.get<>()`, `.get_ref<>()`, and
// `.get_ptr<>()` here:
// 1. https://json.nlohmann.me/api/basic_json/get/
// 1. https://json.nlohmann.me/api/basic_json/get_ref/
// 1. https://json.nlohmann.me/api/basic_json/get_ptr/
printf("name: %s\n", j1["name"].get_ref<const std::string&>().c_str());
printf("status: %u\n", j1["status"].get<bool>());
printf("pi: %f\n", j1["pi"].get<double>());
printf("\n");
// Print the whole object at once as a json string blob
std::cout << j1.dump() << "\n"; // C++-style
printf("%s\n", j1.dump().c_str()); // C-style
printf("\n");
// Store into a string first, then print
std::string j1str = j1.dump();
std::cout << j1str << "\n"; // C++-style
printf("%s\n", j1str.c_str()); // C-style
printf("\n");
// Pretty print; pass in the number of spaces to indent
std::cout << j1.dump(4) << "\n"; // C++-style
printf("%s\n", j1.dump(4).c_str()); // C-style
printf("\n");
return 0;
}
/*
SAMPLE OUTPUT:
eRCaGuy_hello_world/cpp$ time ccache g++ -Wall -Wextra -Werror -O3 -std=c++17 -I"json/single_include" json_nlohmann_demo.cpp -o bin/a && bin/a
real 0m2.971s
user 0m2.856s
sys 0m0.115s
Hello world!
name: "John"
status: true
pi: 3.141
name: John
status: 1
pi: 3.141000
{"name":"John","pi":3.141,"status":true}
{"name":"John","pi":3.141,"status":true}
{"name":"John","pi":3.141,"status":true}
{"name":"John","pi":3.141,"status":true}
{
"name": "John",
"pi": 3.141,
"status": true
}
{
"name": "John",
"pi": 3.141,
"status": true
}
*/