nlohmann json for modern C++

Posted kaizen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nlohmann json for modern C++相关的知识,希望对你有一定的参考价值。

【1】作者简介

【2】库

https://github.com/nlohmann/json

【3】应用示例

(1)工程配置

(2)示例代码

  1 #include <string>
  2 #include <vector>
  3 #include <fstream>
  4 #include <iostream>
  5 #include <iomanip>
  6 
  7 // 为了调试中文显示
  8 #pragma execution_character_set("utf-8")
  9 
 10 #include "nlohmann/json.hpp"
 11 // for convenience
 12 using json = nlohmann::json;
 13 
 14 void json_object()
 15 {
 16     // create a JSON object
 17     json j =
 18     {
 19         {"pi", 3.141},
 20         {"happy", true},
 21         {"name", "Niels"},
 22         {"nothing", nullptr},
 23         {
 24             "answer", {
 25                 {"everything", 42}
 26             }
 27         },
 28         {"list", {1, 0, 2}},
 29         {
 30             "object", {
 31                 {"currency", "USD"},
 32                 {"value", 42.99}
 33             }
 34         }
 35     };
 36 
 37     // add new values
 38     j["new"]["key"]["value"] = { "another", "list" };
 39 
 40     // count elements
 41     auto s = j.size();
 42     j["size"] = s;
 43 
 44     // pretty print with indent of 4 spaces
 45     std::cout << std::setw(4) << j << \'\\n\';
 46 
 47     // create JSON values
 48     json j_boolean = true;
 49     json j_number_integer = 17;
 50     json j_number_float = 23.42;
 51     json j_object = { {"one", 1}, {"two", 2} };
 52     json j_object_empty(json::value_t::object);
 53     json j_array = { 1, 2, 4, 8, 16 };
 54     json j_array_empty(json::value_t::array);
 55     json j_string = "Hello, world";
 56 
 57     // call back()
 58     std::cout << j_boolean.back() << \'\\n\';
 59     std::cout << j_number_integer.back() << \'\\n\';
 60     std::cout << j_number_float.back() << \'\\n\';
 61     std::cout << j_object.back() << \'\\n\';
 62     //std::cout << j_object_empty.back() << \'\\n\';  // undefined behavior
 63     std::cout << j_array.back() << \'\\n\';
 64     //std::cout << j_array_empty.back() << \'\\n\';   // undefined behavior
 65     std::cout << j_string.back() << \'\\n\';
 66 
 67     // back() called on a null value
 68     try
 69     {
 70         json j_null;
 71         j_null.back();
 72     }
 73     catch (json::invalid_iterator & e)
 74     {
 75         std::cout << e.what() << \'\\n\';
 76     }
 77 }
 78 
 79 void json_arry()
 80 {
 81     // create JSON arrays
 82     json j_no_init_list = json::array();
 83     json j_empty_init_list = json::array({});
 84     json j_nonempty_init_list = json::array({ 1, 2, 3, 4 });
 85     json j_list_of_pairs = json::array({ {"one", 1}, {"two", 2} });
 86 
 87     // serialize the JSON arrays
 88     std::cout << j_no_init_list << \'\\n\';
 89     std::cout << j_empty_init_list << \'\\n\';
 90     std::cout << j_nonempty_init_list << \'\\n\';
 91     std::cout << j_list_of_pairs << \'\\n\';
 92 }
 93 
 94 void json_object_at_key()
 95 {
 96     // create JSON object
 97     json object =
 98     {
 99         {"the good", "il buono"},
100         {"the bad", "il cattivo"},
101         {"the ugly", "il brutto"}
102     };
103 
104     // output element with key "the ugly"
105     std::cout << object.at("the ugly") << \'\\n\';
106 
107     // change element with key "the bad"
108     object.at("the bad") = "liuy kaizen";
109     object.at("the good") = 100;
110 
111     // output changed array
112     std::cout << object << \'\\n\';
113 
114     // exception type_error.304
115     try
116     {
117         // use at() on a non-object type
118         json j_str = "I am a string";
119         std::cout << j_str << \'\\n\';
120         j_str.at("the good") = "Another string";
121 
122         const json jc_str = "I am a const json object";
123         std::cout << jc_str << "\\n";
124         // jc_str.at("the good") = "Const string"; const对象,禁止赋值
125     }
126     catch (json::type_error & e)
127     {
128         std::cout << e.what() << \'\\n\';
129     }
130 
131     // exception out_of_range.401
132     try
133     {
134         // try to write at a nonexisting key
135         object.at("the fast") = "il rapido";
136     }
137     catch (json::out_of_range & e)
138     {
139         std::cout << e.what() << \'\\n\';
140     }
141 }
142 
143 void json_array_at_index()
144 {
145     // create JSON array
146     json array = { "first", "2nd", "third", "fourth" };
147 
148     // output element at index 2 (third element)
149     std::cout << array.at(2) << \'\\n\';
150 
151     // change element at index 1 (second element) to "second"
152     array.at(1) = "second";
153 
154     // output changed array
155     std::cout << array << \'\\n\';
156 
157     // exception type_error.304
158     try
159     {
160         // use at() on a non-array type
161         json str = "I am a string";
162         str.at(0) = "Another string";
163     }
164     catch (json::type_error & e)
165     {
166         std::cout << e.what() << \'\\n\';
167     }
168 
169     // exception out_of_range.401
170     try
171     {
172         // try to write beyond the array limit
173         array.at(5) = "sixth";
174     }
175     catch (json::out_of_range & e)
176     {
177         std::cout << e.what() << \'\\n\';
178     }
179 }
180 
181 void json_at_pointer()
182 {
183     // create a JSON value
184     json j =
185     {
186         {"number", 1}, {"string", "foo"}, {"array", {1, 2}}
187     };
188 
189     // read-only access
190 
191     // output element with JSON pointer "/number"
192     std::cout << j.at("/number"_json_pointer) << \'\\n\';
193     // output element with JSON pointer "/string"
194     std::cout << j.at("/string"_json_pointer) << \'\\n\';
195     // output element with JSON pointer "/array"
196     std::cout << j.at("/array"_json_pointer) << \'\\n\';
197     // output element with JSON pointer "/array/0"
198     std::cout << j.at("/array/0"_json_pointer) << \'\\n\';
199     // output element with JSON pointer "/array/1"
200     std::cout << j.at("/array/1"_json_pointer) << \'\\n\';
201 
202     // writing access
203 
204     // change the string
205     j.at("/string"_json_pointer) = "bar";
206     // output the changed string
207     std::cout << j["string"] << \'\\n\';
208 
209     // change an array element
210     j.at("/array/1"_json_pointer) = 21;
211     // output the changed array
212     std::cout << j["array"] << \'\\n\';
213 
214     // out_of_range.106
215     try
216     {
217         // try to use an array index with leading \'0\'
218         json::reference ref = j.at("/array/01"_json_pointer);
219     }
220     catch (json::parse_error & e)
221     {
222         std::cout << e.what() << \'\\n\';
223     }
224 
225     // out_of_range.109
226     try
227     {
228         // try to use an array index that is not a number
229         json::reference ref = j.at("/array/one"_json_pointer);
230     }
231     catch (json::parse_error & e)
232     {
233         std::cout << e.what() << \'\\n\';
234     }
235 
236     // out_of_range.401
237     try
238     {
239         // try to use a an invalid array index
240         json::reference ref = j.at("/array/4"_json_pointer);
241     }
242     catch (json::out_of_range & e)
243     {
244         std::cout << e.what() << \'\\n\';
245     }
246 
247     // out_of_range.402
248     try
249     {
250         // try to use the array index \'-\'
251         json::reference ref = j.at("/array/-"_json_pointer);
252     }
253     catch (json::out_of_range & e)
254     {
255         std::cout << e.what() << \'\\n\';
256     }
257 
258     // out_of_range.403
259     try
260     {
261         // try to use a JSON pointer to an nonexistent object key
262         json::const_reference ref = j.at("/foo"_json_pointer);
263     }
264     catch (json::out_of_range & e)
265     {
266         std::cout << e.what() << \'\\n\';
267     }
268 
269     // out_of_range.404
270     try
271     {
272         // try to use a JSON pointer that cannot be resolved
273         json::reference ref = j.at("/number/foo"_json_pointer);
274     }
275     catch (json::out_of_range & e)
276     {
277         std::cout << e.what() << \'\\n\';
278     }
279 }
280 
281 void json_compare()
282 {
283     // create a JSON array
284     json j1 = { "one", "two", 3, 4.5, false };
285 
286     // create a copy
287     json j2(j1);
288 
289     // serialize the JSON array
290     std::cout << j1 << " = " << j2 << \'\\n\';
291     std::cout << std::boolalpha << (j1 == j2) << \'\\n\';
292 }
293 
294 void json_list_init()
295 {
296     // create JSON values
297     json j_empty_init_list = json({});
298     json j_object = { {"one", 1}, {"two", 2} };
299     json j_array = { 1, 2, 3, 4 };
300     json j_nested_object = { {"one", {1}}, {"two", {1, 2}} };
301     json j_nested_array = { {{1}, "one"}, {{1, 2}, "two"} };
302 
303     // serialize the JSON value
304     std::cout << j_empty_init_list << \'\\n\';    // {}
305     std::cout << j_object << \'\\n\';             // {"one":1,"two":2}
306     std::cout << j_array << \'\\n\';              // [1,2,3,4]
307     std::cout << j_nested_object << \'\\n\';      // {"one":[1],"two":[1,2]}
308     std::cout << j_nested_array << \'\\n\';       // [[[1],"one"],[1,2],"two"]
309 }
310 
311 void json_move_constructor()
312 {
313     // create a JSON value
314     json a = 23;
315 
316     // move contents of a to b
317     json b(std::move(a));
318 
319     // serialize the JSON arrays
320     std::cout << a << \'\\n\';    // null
321     std::cout << b << \'\\n\';    // 23
322 }
323 
324 void json_copy_assignment()
325 {
326     // create JSON values
327     json a = 23;
328     json b = 42;
329 
330     // copy-assign a to b
331     b = a;
332 
333     // serialize the JSON arrays
334     std::cout << a << \'\\n\';    // 23
335     std::cout << b << \'C++ 下使用的 JSON 库 JSON for Modern C++ | 软件推介

简单好用的C++ json库——JSON for Modern C++

c++11:nlohmann::json进阶使用ordered_json

c++11:nlohmann::json进阶使用使用basic_json模板类

nlohmann/json uWebsocket

C++ nlohmann/json 如何使用运行时提供的 json_pointers 来读取 json 值