mysvac-jsonlib 2.1.1
一个轻量高校的C++17 JSON解析库
 
载入中...
搜索中...
未找到
jsonlib.cpp
浏览该文件的文档.
1#include "jsonlib.h"
2#include <initializer_list>
3#include <string>
4#include <string_view>
5#include <map>
6#include <vector>
7#include <sstream>
8#include <variant>
9#include <stdexcept>
10
11namespace Jsonlib{
12
16 static void json_escape_unicode(std::string& res, std::string_view str, std::string_view::const_iterator& it) {
17 // 进入时 it 在 \uXXXX 的 u 的位置。
18 if (str.end() - it <= 4) throw JsonStructureException{ "Illegal unicode.\n" };
19 ++it;
20 std::istringstream iss( std::string(str.substr(it-str.begin(), 4)));
21 // 函数返回时,it应该在\uABCD 的 D位置,所以只能 +3
22 it += 3;
23 unsigned int codePoint;
24 iss >> std::hex >> codePoint;
25
26 if (iss.fail() || codePoint > 0xFFFF) {
27 // 错误的\u转义字符,会直接跳过,不会报错。
28 throw JsonStructureException{ "Illegal unicode.\n" };
29 }
30
31 // [0xD800 , 0xE000) 范围,是代理对,是连续2波\u转码
32 if (codePoint >= 0xD800 && codePoint <= 0xDFFF) {
33 // 代理队,必须是 高代理 + 低代理
34 // 高代理 [\uD800, \uDBFF]
35 // 低代理 [\uDC00, \uDFFF]
36 if (codePoint >= 0xDC00) {
37 // 低代理开头,直接结束
38 throw JsonStructureException{ "Illegal unicode - start with low-code.\n" };
39 }
40
41 // 检查下一个转义序列是否是低代理
42 if (str.end() - it < 7 || *(it+1) != '\\' || *(it+2) != 'u') {
43 // 当前是高代理,但是下个位置不是低代理,也直接返回
44 throw JsonStructureException{ "Illegal unicode - only high-code.\n" };
45 }
46
47 // 解析低代理 +3 进入 \uABCD 的 A位置
48 it += 3;
49 std::istringstream lowIss( std::string(str.substr(it-str.begin(), 4) ));
50 it += 3; // 移动到 \uABCD的D位置
51
52 unsigned int lowCodePoint;
53 lowIss >> std::hex >> lowCodePoint;
54
55 if (lowIss.fail() || lowCodePoint < 0xDC00 || lowCodePoint > 0xDFFF) {
56 // 不是低代理对,说明错误
57 throw JsonStructureException{ "Illegal unicode - not end with lowcode.\n" };
58 }
59
60 // 将代理对组合为单个码点
61 codePoint = 0x10000 + ((codePoint - 0xD800) << 10) + (lowCodePoint - 0xDC00);
62 }
63
64 // 将码点编码为 UTF-8
65 if (codePoint <= 0x7F) {
66 res += static_cast<char>(codePoint);
67 }
68 else if (codePoint <= 0x7FF) {
69 res += static_cast<char>(0xC0 | (codePoint >> 6));
70 res += static_cast<char>(0x80 | (codePoint & 0x3F));
71 }
72 else if (codePoint <= 0xFFFF) {
73 res += static_cast<char>(0xE0 | (codePoint >> 12));
74 res += static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F));
75 res += static_cast<char>(0x80 | (codePoint & 0x3F));
76 }
77 else if (codePoint <= 0x10FFFF) {
78 res += static_cast<char>(0xF0 | (codePoint >> 18));
79 res += static_cast<char>(0x80 | ((codePoint >> 12) & 0x3F));
80 res += static_cast<char>(0x80 | ((codePoint >> 6) & 0x3F));
81 res += static_cast<char>(0x80 | (codePoint & 0x3F));
82 }
83 else throw JsonStructureException{ "Illegal unicode.\n" };;
84 }
85
89 static std::string json_escape_next(std::string_view str, std::string_view::const_iterator& it) {
90 // 跳过字符串起始的双引号
91 ++it;
92 std::string res;
93
94 while (it != str.end() && *it != '\"') {
95 switch (*it) {
96 case '\\':
97 {
98 // 转义字符处理
99 ++it;
100 if (it == str.end()) throw JsonStructureException{ "String have not end char '\"'." };
101 switch (*it)
102 {
103 case '\"':
104 res += '\"';
105 break;
106 case '\\':
107 res += '\\';
108 break;
109 case '/':
110 res += '/';
111 break;
112 case 'n':
113 res += '\n';
114 break;
115 case 'r':
116 res += '\r';
117 break;
118 case 't':
119 res += '\t';
120 break;
121 case 'f':
122 res += '\f';
123 break;
124 case 'b':
125 res += '\b';
126 break;
127 case 'u':
128 case 'U':
129 json_escape_unicode(res, str, it);
130 break;
131 default:
132 throw JsonStructureException{ "Illegal escape characters.\n " };
133 }
134 }
135 break;
136 case '\t':
137 case '\n':
138 case '\f':
139 case '\b':
140 case '\r':
141 throw JsonStructureException{ "There are characters that have not been escaped.\n" };
142 default:
143 res += *it;
144 break;
145 }
146 ++it;
147 }
148 if(it == str.end()) throw JsonStructureException {"Unclosed string.\n"};
149 ++it;
150 return res;
151 }
152
153
157 static std::string json_reverse_escape(std::string_view str) noexcept {
158 std::string res;
159 // 提前分配空间,减少扩容开销
160 if (str.size() > 15) res.reserve(str.size() + (str.size() >> 4));
161 res += "\"";
162 for (const char& it : str) {
163 switch (it) {
164 case '\"':
165 res += "\\\"";
166 break;
167 case '\\':
168 res += "\\\\";
169 case '\n':
170 res += "\\n";
171 break;
172 case '\f':
173 res += "\\f";
174 break;
175 case '\t':
176 res += "\\t";
177 break;
178 case '\r':
179 res += "\\r";
180 break;
181 case '\b':
182 res += "\\b";
183 break;
184 default:
185 res += it;
186 break;
187 }
188 }
189 res += "\"";
190 return res;
191 }
192
196 static std::string json_escape(std::string_view str) {
197 auto it = str.begin();
198 ++it;
199 std::string res;
200
201 while (it != str.end() && *it != '\"') {
202 switch (*it) {
203 case '\\':
204 {
205 // 转义字符处理
206 ++it;
207 if (it == str.end()) throw JsonStructureException{ "String have not end char '\"'." };
208 switch (*it)
209 {
210 case '\"':
211 res += '\"';
212 break;
213 case '\\':
214 res += '\\';
215 break;
216 case '/':
217 res += '/';
218 break;
219 case 'n':
220 res += '\n';
221 break;
222 case 'r':
223 res += '\r';
224 break;
225 case 't':
226 res += '\t';
227 break;
228 case 'f':
229 res += '\f';
230 break;
231 case 'b':
232 res += '\b';
233 break;
234 case 'u':
235 case 'U':
236 json_escape_unicode(res, str, it);
237 break;
238 default:
239 break;
240 }
241 }
242 break;
243 default:
244 res += *it;
245 break;
246 }
247 ++it;
248 }
249 return res;
250 }
251
252 // 指定类型的构造函数
253 JsonValue::JsonValue(const JsonType& jsonType) {
254 type_ = jsonType;
255 switch (jsonType)
256 {
257 case JsonType::OBJECT:
259 break;
260 case JsonType::ARRAY:
262 break;
263 case JsonType::STRING:
264 content_ = std::string{ "\"\"" };
265 break;
266 case JsonType::NUMBER:
267 content_ = std::string{ "0" };
268 break;
269 default:
270 break;
271 }
272 }
273
274 // 反序列化
275 JsonValue deserialize(std::string_view str){
276 auto it = str.begin();
277 while(it!=str.end() && std::isspace(*it)) ++it;
278
279 // 禁止空内容
280 if(it == str.end()){
281 throw JsonStructureException{ "Empty JSON data.\n" };
282 }
283
284 JsonValue jsonValue (str, it);
285
286 while( it != str.end() ){
287 if(!std::isspace(*it)) throw JsonStructureException {"Unknown content at the end.\n"};
288 ++it;
289 }
290 return jsonValue;
291 }
292
293 // 反序列化构造
294 JsonValue::JsonValue(std::string_view str, std::string_view::const_iterator& it){
295 while(it != str.end() && std::isspace(*it)) ++it;
296 if(it == str.end()) throw JsonStructureException{ "Empty JSON data.\n" };
297 switch (*it){
298 case '{':
299 {
300 type_ = JsonType::OBJECT;
301 content_ = JsonObject {};
302 auto& jsonObject = std::get<JsonObject>(content_);
303 std::string key;
304 ++it;
305 while(it != str.end()){
306 while (it!=str.end() && std::isspace(*it)) ++it;
307 if(it == str.end() || *it == '}') break;
308 // 寻找key
309 if (*it != '\"') throw JsonStructureException{ "Key is not string.\n" };
310 key = json_escape_next(str, it);
311 // 寻找分号
312 while (it != str.end() && std::isspace(*it)) ++it;
313 if(it == str.end()) throw JsonStructureException{ "Illegal Json Object content.\n" };
314 if (*it == ':') ++it;
315 else throw JsonStructureException {};
316 // 寻找值
317 while (it != str.end() && std::isspace(*it)) ++it;
318 jsonObject[std::move(key)] = JsonValue ( str, it );
319 // 寻找分隔符或结束位
320 while (it != str.end() && std::isspace(*it)) ++it;
321 if (it != str.end() && *it == ',') ++it;
322 }
323 if(it == str.end()) throw JsonStructureException{ "Unclosed Json Object.\n" };
324 else ++it;
325 }
326 break;
327 case '[':
328 {
329 type_ = JsonType::ARRAY;
331 auto& jsonArray = std::get<JsonArray>(content_);
332 ++it;
333 while(it!=str.end()){
334 while (it!=str.end() && std::isspace(*it)) ++it;
335 if(it == str.end() || *it == ']') break;
336 // 寻找值
337 jsonArray.emplace_back( str, it );
338 // 寻找分隔符或结束位
339 while (it != str.end() && std::isspace(*it)) ++it;
340 if (it != str.end() && *it == ',') ++it;
341 }
342 if(it==str.end()) throw JsonStructureException{ "Unclosed Json Object.\n" };
343 else ++it;
344 }
345 break;
346 case '\"':
347 {
348 type_ = JsonType::STRING;
349 auto left = it;
350 ++it;
351 while(it != str.end() && *it != '\"'){
352 if(*it == '\\'){
353 ++it;
354 if(it == str.end()) break;
355 }
356 ++it;
357 }
358 if(it == str.end()) throw JsonStructureException {"Unclosed string.\n"};
359 content_ = std::string (left, ++it);
360 }
361 break;
362 case 't':
363 if(str.end() - it < 4 || str.compare(it-str.begin(), 4, "true")) throw JsonStructureException {};
364 type_ = JsonType::BOOL;
365 content_ = true;
366 it += 4;
367 break;
368 case 'f':
369 if(str.end() - it < 5 || str.compare(it-str.begin(), 5, "false")) throw JsonStructureException {};
370 type_ = JsonType::BOOL;
371 it += 5;
372 break;
373 case 'n':
374 if(str.end() - it < 4 || str.compare(it-str.begin(), 4, "null")) throw JsonStructureException {};
375 it += 4;
376 break;
377 default:
378 {
379 type_ = JsonType::NUMBER;
380 bool have_not_point = true;
381 bool have_not_e = true;
382 auto left = it;
383 if(*it == '-') ++it;
384 // 必须数字开头
385 if(it == str.end() || !std::isdigit(*it)) throw JsonStructureException {};
386 while (it != str.end()) {
387 if (std::isdigit(*it)) ++it;
388 else if (*it == '.' && have_not_point && have_not_e) {
389 have_not_point = false;
390 ++it;
391 }
392 else if ((*it == 'e' || *it == 'E') && have_not_e) {
393 have_not_e = false;
394 ++it;
395 if (it != str.end() && (*it == '-' || *it == '+')) ++it;
396 }
397 else break;
398 }
399 if(it == left || (*left=='-' && it==left+1)) throw JsonStructureException {};
400 content_ = std::string (left, it);
401 }
402 break;
403 }
404 }
405
406 // 字符串类型构造
407 JsonValue::JsonValue(std::string_view str) noexcept{
408 type_ = JsonType::STRING;
409 content_ = json_reverse_escape(str);
410 }
411
412 // 字符串类型赋值
413 JsonValue& JsonValue::operator=(std::string_view str) {
414 type_ = JsonType::STRING;
416 return *this;
417 }
418
419 // 字符串字面量构造
420 JsonValue::JsonValue(const char* str) noexcept{
421 type_ = JsonType::STRING;
422 content_ = json_reverse_escape(str);
423 }
424 // 字符串字面量赋值
425 JsonValue& JsonValue::operator=(const char* str) noexcept{
426 type_ = JsonType::STRING;
427 content_ = json_reverse_escape(str);
428 return *this;
429 }
430
431 // 列表初始化器
432 JsonValue::JsonValue(const std::initializer_list<JsonValue>& init_list) {
433 if (init_list.size() == 0) return;
434 if (init_list.size() == 2 && init_list.begin()->is_string()){
435 type_ = JsonType::OBJECT;
437 auto& map = std::get<JsonObject>(content_);
438 map[init_list.begin()->as_string()] = init_list.begin()[1];
439 }
440 else {
441 type_ = JsonType::ARRAY;
443 auto& list = std::get<JsonArray>(content_);
444 list.reserve(init_list.size());
445 for (const auto& it : init_list) {
446 list.push_back(it);
447 }
448 }
449 }
450
451 // 清空内容
452 void JsonValue::clear() noexcept {
453 switch (type_)
454 {
455 case JsonType::OBJECT:
457 break;
458 case JsonType::ARRAY:
460 break;
461 case JsonType::STRING:
462 content_ = std::string{ "\"\"" };
463 break;
464 case JsonType::NUMBER:
465 content_ = std::string{ "0" };
466 break;
467 case JsonType::BOOL:
468 content_ = false;
469 break;
470 default:
471 break;
472 }
473 }
474
475 // 长度获取
476 size_t JsonValue::size() const noexcept{
477 switch (type_)
478 {
479 case JsonType::OBJECT:
480 return std::get<JsonObject>(content_).size();
481 case JsonType::ARRAY:
482 return std::get<JsonArray>(content_).size();
483 case JsonType::STRING:
484 return std::get<std::string>(content_).size();
485 default:
486 return 1;
487 }
488 }
489
490
491 // 拷贝构造
492 JsonValue::JsonValue(const JsonValue& jsonValue) noexcept{
493 type_ = jsonValue.type_;
494 content_ = jsonValue.content_;
495 }
496
497 //移动构造
498 JsonValue::JsonValue(JsonValue&& jsonValue) noexcept{
499 type_ = jsonValue.type_;
500 content_ = std::move(jsonValue.content_);
501 jsonValue.reset();
502 }
503
504 // 拷贝赋值
505 JsonValue& JsonValue::operator=(const JsonValue& jsonValue) noexcept{
506 if (this == &jsonValue) return *this;
507 type_ = jsonValue.type_;
508 content_ = jsonValue.content_;
509 return *this;
510 }
511
512 // 移动赋值
514 if (this == &jsonValue) return *this;
515 type_ = jsonValue.type_;
516 content_ = std::move(jsonValue.content_);
517 jsonValue.reset();
518 return *this;
519 }
520
521
522 // JsonArray拷贝构造
523 JsonValue::JsonValue(const JsonArray& jsonArray) noexcept{
524 type_ = JsonType::ARRAY;
525 content_ = jsonArray;
526 }
527
528 // JsonArray移动构造
529 JsonValue::JsonValue(JsonArray&& jsonArray) noexcept{
530 type_ = JsonType::ARRAY;
531 content_ = std::move(jsonArray);
532 jsonArray.clear();
533 }
534
535 // JsonObject拷贝构造
536 JsonValue::JsonValue(const JsonObject& jsonObject) noexcept{
537 type_ = JsonType::OBJECT;
538 content_ = jsonObject;
539 }
540
541 // JsonObject移动构造
542 JsonValue::JsonValue(JsonObject&& jsonObject) noexcept{
543 type_ = JsonType::OBJECT;
544 content_ = std::move(jsonObject);
545 jsonObject.clear();
546 }
547
548
549 // JsonArray拷贝赋值
550 JsonValue& JsonValue::operator=(const JsonArray& jsonArray) noexcept{
551 if (type_ == JsonType::ARRAY && &jsonArray == &std::get<JsonArray>(content_))
552 return *this;
553
554 type_ = JsonType::ARRAY;
555 content_ = jsonArray;
556 return *this;
557 }
558
559 // JsonArray移动赋值
561 if (type_ == JsonType::ARRAY && &jsonArray == &std::get<JsonArray>(content_))
562 return *this;
563
564 type_ = JsonType::ARRAY;
565 content_ = std::move(jsonArray);
566 jsonArray.clear();
567 return *this;
568 }
569
570 // JsonObject拷贝赋值
571 JsonValue& JsonValue::operator=(const JsonObject& jsonObject) noexcept{
572 if (type_ == JsonType::OBJECT && &jsonObject == &std::get<JsonObject>(content_))
573 return *this;
574
575 type_ = JsonType::OBJECT;
576 content_ = jsonObject;
577 return *this;
578 }
579
580 // JsonObject移动赋值
582 if (type_ == JsonType::OBJECT && &jsonObject == &std::get<JsonObject>(content_))
583 return *this;
584
585 type_ = JsonType::OBJECT;
586 content_ = std::move(jsonObject);
587 jsonObject.clear();
588 return *this;
589 }
590
591
592
593 // 序列化JSON对象
594 std::string JsonValue::serialize() const noexcept {
595 switch (type_)
596 {
597 case JsonType::OBJECT:
598 {
599 // 对象类型
600 // 是否在开头加上逗号
601 std::string res{ "{" };
602 const auto& map = std::get<JsonObject>(content_);
603 for (const auto& [fst, snd] : map) {
604 // 键是字符串,需要反转义
605 res += json_reverse_escape(fst);
606 res += ':';
607 // 递归序列号
608 res += snd.serialize();
609 res += ',';
610 }
611 if (*res.rbegin() == ',') *res.rbegin() = '}';
612 else res += '}';
613 return res;
614 }
615 case JsonType::ARRAY:
616 {
617 // 数组类型
618 // 是否在开头加上逗号
619 std::string res{ "[" };
620 const auto& list = std::get<JsonArray>(content_);
621 for (const JsonValue& it : list) {
622 // 递归序列号
623 res += it.serialize();
624 res += ',';
625 }
626 if (*res.rbegin() == ',') *res.rbegin() = ']';
627 else res += ']';
628 return res;
629 }
630 case JsonType::BOOL:
631 return std::get<bool>(content_) ? "true" : "false";
632 case JsonType::ISNULL:
633 return "null";
634 default:
635 // 数值和字符串,可以直接返回内容
636 return std::get<std::string>(content_);
637 }
638 }
639
640 // 序列化JSON对象含空格和换行
641 std::string JsonValue::serialize_pretty(const size_t& space_num, const size_t& depth) const noexcept {
642 switch (type_)
643 {
644 case JsonType::OBJECT:
645 {
646 // 对象类型
647 std::string res{ "{" };
648 int tabs = depth * space_num + space_num;
649 const auto& map = std::get<JsonObject>(content_);
650 for (const auto& [fst, snd] : map) {
651 res += '\n';
652 res.append(tabs, ' ');
653 // 键是字符串,需要反转义
654 res += json_reverse_escape(fst);
655 res += ": ";
656 res += snd.serialize_pretty(space_num, depth + 1);
657 res += ',';
658 }
659 if (*res.rbegin() == ',') *res.rbegin() = '\n';
660 if(!map.empty()){
661 res.append(tabs - space_num, ' ');
662 res += '}';
663 }
664 else res += " }";
665 return res;
666 }
667 case JsonType::ARRAY:
668 {
669 // 数组类型
670 std::string res{ "[" };
671 int tabs = depth * space_num + space_num;
672 const auto& list = std::get<JsonArray>(content_);
673 for (const JsonValue& it : list) {
674 res += '\n';
675 res.append(tabs, ' ');
676 res += it.serialize_pretty(space_num, depth + 1);
677 res += ',';
678 }
679 if (*res.rbegin() == ',') *res.rbegin() = '\n';
680 if(!list.empty()){
681 res.append(tabs - space_num, ' ');
682 res += ']';
683 }
684 else res += " ]";
685 return res;
686 }
687 case JsonType::BOOL:
688 return std::get<bool>(content_) ? "true" : "false";
689 case JsonType::ISNULL:
690 return "null";
691 default:
692 // 数值和字符串,可以直接返回内容
693 return std::get<std::string>(content_);
694 }
695 }
696
697 // as 赋值内容或对象
698 long long JsonValue::as_int64() const {
699 if (type_ != JsonType::NUMBER) throw JsonTypeException{ "Is not Number.\n" };
700 return std::stoll(std::get<std::string>(content_));
701 }
702 double JsonValue::as_double() const {
703 if (type_ != JsonType::NUMBER) throw JsonTypeException{ "Is not Number.\n" };
704 return std::stod(std::get<std::string>(content_));
705 }
706 long double JsonValue::as_ldouble() const{
707 if (type_ != JsonType::NUMBER) throw JsonTypeException{ "Is not Number.\n" };
708 return std::stold(std::get<std::string>(content_));
709 }
710 bool JsonValue::as_bool() const {
711 if (type_ != JsonType::BOOL) throw JsonTypeException{ "Is not bool.\n" };
712 return std::get<bool>(content_);
713 }
714 std::string JsonValue::as_string() const {
715 if (type_ == JsonType::ARRAY || type_ == JsonType::OBJECT) throw JsonTypeException{ "Is not String.\n" };
716 switch (type_)
717 {
718 case JsonType::STRING:
719 return json_escape(std::get<std::string>(content_));
720 case JsonType::BOOL:
721 return std::get<bool>(content_) ? "true" : "false";
722 case JsonType::ISNULL:
723 return "null";
724 default:
725 // 数值,可以直接返回内容
726 return std::get<std::string>(content_);
727 }
728
729 }
731 if (type_ != JsonType::OBJECT) throw JsonTypeException{ "Is not object.\n" };
732 return std::get<JsonObject>(content_);
733 }
735 if (type_ != JsonType::OBJECT) throw JsonTypeException{ "Is not object.\n" };
736 return std::get<JsonObject>(content_);
737 }
739 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not object.\n" };
740 return std::get<JsonArray>(content_);
741 }
743 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not object.\n" };
744 return std::get<JsonArray>(content_);
745 }
746 // 列表访问,拒绝新元素
747 JsonValue& JsonValue::at(const size_t& index) {
748 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not array.\n" };
749 auto& list = std::get<JsonArray>(content_);
750 if (index < 0 || index >= list.size()) throw std::out_of_range{ "out of range.\n" };
751 return list.at(index);
752 }
753 // 对象访问,拒绝新元素
754 JsonValue& JsonValue::at(const std::string& key) {
755 if (type_ != JsonType::OBJECT) throw JsonTypeException{ "Is not Object.\n" };
756 auto& map = std::get<JsonObject>(content_);
757 if (auto it = map.find(key); it == map.end()) throw std::out_of_range{ + "Key not find.\n" };
758 else return it->second;
759 }
760 // 列表访问,可能创建新元素
761 JsonValue& JsonValue::operator[](const size_t& index) {
762 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not array.\n" };
763 auto& list = std::get<JsonArray>(content_);
764 if (index == list.size()) list.emplace_back();
765 return list[index];
766 }
767 // 对象访问,可能创建新元素
768 JsonValue& JsonValue::operator[](const std::string& key) {
769 if (type_ != JsonType::OBJECT) throw JsonTypeException{ "Is not Object.\n" };
770 auto& map = std::get<JsonObject>(content_);
771 return map[key];
772 }
773
774 // 检查是否包含某个key
775 bool JsonValue::hasKey(const std::string& key) const noexcept{
776 if (type_ != JsonType::OBJECT) return false;
777 const auto& map = std::get<JsonObject>(content_);
778 return map.find(key) != map.end();
779 }
780 // 数组末尾插入元素
781 void JsonValue::push_back(const JsonValue& jsonValue) {
782 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not Array.\n" };
783 auto& list = std::get<JsonArray>(content_);
784 list.push_back(jsonValue);
785 }
786 // 数组末尾移动进入元素
787 void JsonValue::push_back(JsonValue&& jsonValue) {
788 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not Array.\n" };
789 auto& list = std::get<JsonArray>(content_);
790 list.push_back(std::move(jsonValue));
791 }
792 // 数组尾部删除元素
794 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not Array.\n" };
795 auto& list = std::get<JsonArray>(content_);
796 list.pop_back();
797 }
798
799 // 数值指定位置插入元素
800 void JsonValue::insert(const size_t& index, const JsonValue& jsonValue) {
801 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not Array.\n" };
802 auto& list = std::get<JsonArray>(content_);
803 if (index < 0 || index > list.size()) throw std::out_of_range{ "out of range.\n" };
804 list.insert(std::next(list.begin(), static_cast<std::ptrdiff_t>(index)), jsonValue);
805 }
806 // 数组指定位置移入元素
807 void JsonValue::insert(const size_t& index, JsonValue&& jsonValue) {
808 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not Array.\n" };
809 auto& list = std::get<JsonArray>(content_);
810 if (index < 0 || index > list.size()) throw std::out_of_range{ "out of range.\n" };
811 list.insert(std::next(list.begin(), static_cast<std::ptrdiff_t>(index)), std::move(jsonValue));
812 }
813 // 对象指定位置插入键值对
814 void JsonValue::insert(const std::string& key, const JsonValue& jsonValue) {
815 if (type_ != JsonType::OBJECT) throw JsonTypeException{ "Is not Object.\n" };
816 auto& map = std::get<JsonObject>(content_);
817 map[key] = jsonValue;
818 }
819 // 对象指定位置移动插入键值对
820 void JsonValue::insert(const std::string& key, JsonValue&& jsonValue) {
821 if (type_ != JsonType::OBJECT) throw JsonTypeException{ "Is not Object.\n" };
822 auto& map = std::get<JsonObject>(content_);
823 map[key] = std::move(jsonValue);
824 }
825 // 数值删除指定位置的元素
826 void JsonValue::erase(const size_t& index) {
827 if (type_ != JsonType::ARRAY) throw JsonTypeException{ "Is not array.\n" };
828 auto& list = std::get<JsonArray>(content_);
829 if (index < 0 || index >= list.size()) throw std::out_of_range{ std::string{__FILE__} + ":" + std::to_string(__LINE__) + " out of range.\n" };
830 list.erase(std::next(list.begin(),static_cast<std::ptrdiff_t>(index)));
831 }
832 // 对象删除指定key的元素
833 void JsonValue::erase(const std::string& key) {
834 if (type_ != JsonType::OBJECT) throw JsonTypeException{ "Is not Object.\n" };
835 auto& map = std::get<JsonObject>(content_);
836 map.erase(key);
837 }
838
839}
Json结构错误异常类
Definition jsonlib.h:76
Json类型错误异常类
Definition jsonlib.h:58
Json数据通用类
Definition jsonlib.h:118
size_t size() const noexcept
获取子元素数量
Definition jsonlib.cpp:476
JsonArray & as_array()
获取内部Array对象的引用
Definition jsonlib.cpp:738
JsonValue & operator[](const size_t &index)
[]元素访问
Definition jsonlib.cpp:761
void pop_back()
末尾删除元素,O(1)
Definition jsonlib.cpp:793
JsonVar content_
存储JSON数据。
Definition jsonlib.h:126
JsonType type_
表示当前对象存储的数据类型,
Definition jsonlib.h:131
std::string serialize() const noexcept
序列化对象
Definition jsonlib.cpp:594
std::string as_string() const
转换成string类型,注意是拷贝一份
Definition jsonlib.cpp:714
void insert(const size_t &index, const JsonValue &jsonBasic)
在指定位置插入元素
Definition jsonlib.cpp:800
JsonValue & operator=(std::string_view str)
字符串赋值函数
Definition jsonlib.cpp:413
std::string serialize_pretty(const size_t &space_num=2, const size_t &depth=0) const noexcept
序列化对象且美化
Definition jsonlib.cpp:641
void push_back(const JsonValue &jsonBasic)
末尾拷贝插入元素,O(1)
Definition jsonlib.cpp:781
void erase(const size_t &index)
删除指定位置的元素
Definition jsonlib.cpp:826
long double as_ldouble() const
转换成double类型,复制一份
Definition jsonlib.cpp:706
JsonValue()=default
默认构造函数
long long as_int64() const
转换成long long类型,复制一份
Definition jsonlib.cpp:698
JsonValue & at(const size_t &index)
at元素访问
Definition jsonlib.cpp:747
bool as_bool() const
转换成bool类型,复制一份
Definition jsonlib.cpp:710
double as_double() const
转换成double类型,复制一份
Definition jsonlib.cpp:702
void clear() noexcept
按类型清除
Definition jsonlib.cpp:452
JsonObject & as_object()
获取内部Object对象的引用
Definition jsonlib.cpp:730
bool hasKey(const std::string &key) const noexcept
检查是否存在某个key
Definition jsonlib.cpp:775
static std::string json_escape(std::string_view str)
内部函数,转义字符串
Definition jsonlib.cpp:196
static std::string json_escape_next(std::string_view str, std::string_view::const_iterator &it)
内部函数,转义字符串且移动指针
Definition jsonlib.cpp:89
std::map< std::string, JsonValue > JsonObject
JsonObject类型
Definition jsonlib.h:96
JsonValue deserialize(std::string_view str)
反序列化函数
Definition jsonlib.cpp:275
std::vector< JsonValue > JsonArray
JsonArray类型
Definition jsonlib.h:102
static std::string json_reverse_escape(std::string_view str) noexcept
内部函数,反转义字符串
Definition jsonlib.cpp:157
static void json_escape_unicode(std::string &res, std::string_view str, std::string_view::const_iterator &it)
内部函数,转义\u字符
Definition jsonlib.cpp:16