1 #include <iostream>
2 #include <deque>
3 #include <list>
4 #include <forward_list>
5 #include <set>
6 #include <unordered_map>
7 #include <unordered_set>
8 #include <valarray>
9 #include <nlohmann/json.hpp>
10
11 using json = nlohmann::json;
12
main()13 int main()
14 {
15 // ============
16 // object types
17 // ============
18
19 // create an object from an object_t value
20 json::object_t object_value = { {"one", 1}, {"two", 2} };
21 json j_object_t(object_value);
22
23 // create an object from std::map
24 std::map<std::string, int> c_map
25 {
26 {"one", 1}, {"two", 2}, {"three", 3}
27 };
28 json j_map(c_map);
29
30 // create an object from std::unordered_map
31 std::unordered_map<const char*, double> c_umap
32 {
33 {"one", 1.2}, {"two", 2.3}, {"three", 3.4}
34 };
35 json j_umap(c_umap);
36
37 // create an object from std::multimap
38 std::multimap<std::string, bool> c_mmap
39 {
40 {"one", true}, {"two", true}, {"three", false}, {"three", true}
41 };
42 json j_mmap(c_mmap); // only one entry for key "three" is used
43
44 // create an object from std::unordered_multimap
45 std::unordered_multimap<std::string, bool> c_ummap
46 {
47 {"one", true}, {"two", true}, {"three", false}, {"three", true}
48 };
49 json j_ummap(c_ummap); // only one entry for key "three" is used
50
51 // serialize the JSON objects
52 std::cout << j_object_t << '\n';
53 std::cout << j_map << '\n';
54 std::cout << j_umap << '\n';
55 std::cout << j_mmap << '\n';
56 std::cout << j_ummap << "\n\n";
57
58 // ===========
59 // array types
60 // ===========
61
62 // create an array from an array_t value
63 json::array_t array_value = {"one", "two", 3, 4.5, false};
64 json j_array_t(array_value);
65
66 // create an array from std::vector
67 std::vector<int> c_vector {1, 2, 3, 4};
68 json j_vec(c_vector);
69
70 // create an array from std::valarray
71 std::valarray<short> c_valarray {10, 9, 8, 7};
72 json j_valarray(c_valarray);
73
74 // create an array from std::deque
75 std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
76 json j_deque(c_deque);
77
78 // create an array from std::list
79 std::list<bool> c_list {true, true, false, true};
80 json j_list(c_list);
81
82 // create an array from std::forward_list
83 std::forward_list<std::int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
84 json j_flist(c_flist);
85
86 // create an array from std::array
87 std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
88 json j_array(c_array);
89
90 // create an array from std::set
91 std::set<std::string> c_set {"one", "two", "three", "four", "one"};
92 json j_set(c_set); // only one entry for "one" is used
93
94 // create an array from std::unordered_set
95 std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
96 json j_uset(c_uset); // only one entry for "one" is used
97
98 // create an array from std::multiset
99 std::multiset<std::string> c_mset {"one", "two", "one", "four"};
100 json j_mset(c_mset); // both entries for "one" are used
101
102 // create an array from std::unordered_multiset
103 std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
104 json j_umset(c_umset); // both entries for "one" are used
105
106 // serialize the JSON arrays
107 std::cout << j_array_t << '\n';
108 std::cout << j_vec << '\n';
109 std::cout << j_valarray << '\n';
110 std::cout << j_deque << '\n';
111 std::cout << j_list << '\n';
112 std::cout << j_flist << '\n';
113 std::cout << j_array << '\n';
114 std::cout << j_set << '\n';
115 std::cout << j_uset << '\n';
116 std::cout << j_mset << '\n';
117 std::cout << j_umset << "\n\n";
118
119 // ============
120 // string types
121 // ============
122
123 // create string from a string_t value
124 json::string_t string_value = "The quick brown fox jumps over the lazy dog.";
125 json j_string_t(string_value);
126
127 // create a JSON string directly from a string literal
128 json j_string_literal("The quick brown fox jumps over the lazy dog.");
129
130 // create string from std::string
131 std::string s_stdstring = "The quick brown fox jumps over the lazy dog.";
132 json j_stdstring(s_stdstring);
133
134 // serialize the JSON strings
135 std::cout << j_string_t << '\n';
136 std::cout << j_string_literal << '\n';
137 std::cout << j_stdstring << "\n\n";
138
139 // ============
140 // number types
141 // ============
142
143 // create a JSON number from number_integer_t
144 json::number_integer_t value_integer_t = -42;
145 json j_integer_t(value_integer_t);
146
147 // create a JSON number from number_unsigned_t
148 json::number_integer_t value_unsigned_t = 17;
149 json j_unsigned_t(value_unsigned_t);
150
151 // create a JSON number from an anonymous enum
152 enum { enum_value = 17 };
153 json j_enum(enum_value);
154
155 // create values of different integer types
156 short n_short = 42;
157 int n_int = -23;
158 long n_long = 1024;
159 int_least32_t n_int_least32_t = -17;
160 uint8_t n_uint8_t = 8;
161
162 // create (integer) JSON numbers
163 json j_short(n_short);
164 json j_int(n_int);
165 json j_long(n_long);
166 json j_int_least32_t(n_int_least32_t);
167 json j_uint8_t(n_uint8_t);
168
169 // create values of different floating-point types
170 json::number_float_t v_ok = 3.141592653589793;
171 json::number_float_t v_nan = NAN;
172 json::number_float_t v_infinity = INFINITY;
173
174 // create values of different floating-point types
175 float n_float = 42.23;
176 float n_float_nan = 1.0f / 0.0f;
177 double n_double = 23.42;
178
179 // create (floating point) JSON numbers
180 json j_ok(v_ok);
181 json j_nan(v_nan);
182 json j_infinity(v_infinity);
183 json j_float(n_float);
184 json j_float_nan(n_float_nan);
185 json j_double(n_double);
186
187 // serialize the JSON numbers
188 std::cout << j_integer_t << '\n';
189 std::cout << j_unsigned_t << '\n';
190 std::cout << j_enum << '\n';
191 std::cout << j_short << '\n';
192 std::cout << j_int << '\n';
193 std::cout << j_long << '\n';
194 std::cout << j_int_least32_t << '\n';
195 std::cout << j_uint8_t << '\n';
196 std::cout << j_ok << '\n';
197 std::cout << j_nan << '\n';
198 std::cout << j_infinity << '\n';
199 std::cout << j_float << '\n';
200 std::cout << j_float_nan << '\n';
201 std::cout << j_double << "\n\n";
202
203 // =============
204 // boolean types
205 // =============
206
207 // create boolean values
208 json j_truth = true;
209 json j_falsity = false;
210
211 // serialize the JSON booleans
212 std::cout << j_truth << '\n';
213 std::cout << j_falsity << '\n';
214 }
215