1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "rd_json_object.h"
17
18 #include <algorithm>
19 #include <cmath>
20 #include <queue>
21
22 #include "doc_errno.h"
23 #include "rd_log_print.h"
24
25 namespace DocumentDB {
26 #ifndef OMIT_cJSON
27 namespace {
IsNumber(const std::string & str)28 bool IsNumber(const std::string &str)
29 {
30 return std::all_of(str.begin(), str.end(), [](char c) {
31 return std::isdigit(c);
32 });
33 }
34 } // namespace
35
ValueObject(bool val)36 ValueObject::ValueObject(bool val)
37 {
38 valueType = ValueType::VALUE_BOOL;
39 boolValue = val;
40 }
41
ValueObject(double val)42 ValueObject::ValueObject(double val)
43 {
44 valueType = ValueType::VALUE_NUMBER;
45 doubleValue = val;
46 }
47
ValueObject(const char * val)48 ValueObject::ValueObject(const char *val)
49 {
50 valueType = ValueType::VALUE_STRING;
51 stringValue = val;
52 }
53
GetValueType() const54 ValueObject::ValueType ValueObject::GetValueType() const
55 {
56 return valueType;
57 }
58
GetBoolValue() const59 bool ValueObject::GetBoolValue() const
60 {
61 return boolValue;
62 }
63
GetIntValue() const64 int64_t ValueObject::GetIntValue() const
65 {
66 return static_cast<int64_t>(std::llround(doubleValue));
67 }
68
GetDoubleValue() const69 double ValueObject::GetDoubleValue() const
70 {
71 return doubleValue;
72 }
73
GetStringValue() const74 std::string ValueObject::GetStringValue() const
75 {
76 return stringValue;
77 }
78
Parse(const std::string & jsonStr,int & errCode,bool caseSensitive,bool isFilter)79 JsonObject JsonObject::Parse(const std::string &jsonStr, int &errCode, bool caseSensitive, bool isFilter)
80 {
81 JsonObject obj;
82 errCode = obj.Init(jsonStr, isFilter);
83 obj.caseSensitive_ = caseSensitive;
84 return obj;
85 }
86
JsonObject()87 JsonObject::JsonObject()
88 {
89 cjson_ = nullptr;
90 }
91
~JsonObject()92 JsonObject::~JsonObject()
93 {
94 if (isOwner_) {
95 cJSON_Delete(cjson_);
96 }
97 }
98
operator ==(const JsonObject & other) const99 bool JsonObject::operator==(const JsonObject &other) const
100 {
101 return (cJSON_Compare(this->cjson_, other.cjson_, true) != 0); // CaseSensitive
102 }
103
IsNull() const104 bool JsonObject::IsNull() const
105 {
106 return (cjson_ == nullptr);
107 }
108
GetType() const109 JsonObject::Type JsonObject::GetType() const
110 {
111 if (cjson_->type == cJSON_Object) {
112 return JsonObject::Type::JSON_OBJECT;
113 } else if (cjson_->type == cJSON_Array) {
114 return JsonObject::Type::JSON_ARRAY;
115 }
116 return JsonObject::Type::JSON_LEAF;
117 }
118
GetDeep()119 int JsonObject::GetDeep()
120 {
121 if (cjson_ == nullptr) {
122 GLOGE("cJson is nullptr,deep is 0");
123 return 0;
124 }
125 if (jsonDeep_ != 0) {
126 return jsonDeep_;
127 }
128 jsonDeep_ = GetDeep(cjson_);
129 return jsonDeep_;
130 }
131
GetDeep(cJSON * cjson)132 int JsonObject::GetDeep(cJSON *cjson)
133 {
134 if (cjson->child == nullptr) {
135 jsonDeep_ = 0;
136 return 0; // leaf node
137 }
138
139 int depth = -1;
140 cJSON *child = cjson->child;
141 while (child != nullptr) {
142 depth = std::max(depth, GetDeep(child) + 1);
143 child = child->next;
144 }
145 jsonDeep_ = depth;
146 return depth;
147 }
148
CheckNumber(cJSON * item)149 int JsonObject::CheckNumber(cJSON *item)
150 {
151 std::queue<cJSON *> cjsonQueue;
152 cjsonQueue.push(item);
153 while (!cjsonQueue.empty()) {
154 cJSON *node = cjsonQueue.front();
155 cjsonQueue.pop();
156 if (node == nullptr) {
157 return -E_INVALID_ARGS;
158 }
159 if (cJSON_IsNumber(node)) { // node is not null all the time
160 double value = cJSON_GetNumberValue(node);
161 if (value > __DBL_MAX__ || value < -__DBL_MAX__) {
162 return -E_INVALID_ARGS;
163 }
164 }
165 if (node->child != nullptr) {
166 cjsonQueue.push(node->child);
167 }
168 if (node->next != nullptr) {
169 cjsonQueue.push(node->next);
170 }
171 }
172 return E_OK;
173 }
174
Init(const std::string & str,bool isFilter)175 int JsonObject::Init(const std::string &str, bool isFilter)
176 {
177 const char *end = NULL;
178 isOwner_ = true;
179 cjson_ = cJSON_ParseWithOpts(str.c_str(), &end, true);
180 if (cjson_ == nullptr) {
181 GLOGE("Json's format is wrong");
182 return -E_INVALID_JSON_FORMAT;
183 }
184
185 if (cjson_->type != cJSON_Object) {
186 GLOGE("after Parse,cjson_'s type is not cJSON_Object");
187 return -E_INVALID_ARGS;
188 }
189
190 int ret = CheckNumber(cjson_);
191 if (ret == -E_INVALID_ARGS) {
192 GLOGE("Int value is larger than double");
193 return -E_INVALID_ARGS;
194 }
195 if (!isFilter) {
196 bool isFirstFloor = true;
197 ret = CheckJsonRepeatField(cjson_, isFirstFloor);
198 if (ret != E_OK) {
199 return ret;
200 }
201 }
202 return E_OK;
203 }
204
CheckJsonRepeatField(cJSON * object,bool isFirstFloor)205 int JsonObject::CheckJsonRepeatField(cJSON *object, bool isFirstFloor)
206 {
207 if (object == nullptr) {
208 return -E_INVALID_ARGS;
209 }
210 int ret = E_OK;
211 int type = object->type;
212 if (type != cJSON_Object && type != cJSON_Array) {
213 return ret;
214 }
215 std::set<std::string> fieldSet;
216 cJSON *subObj = object->child;
217 while (subObj != nullptr) {
218 ret = CheckSubObj(fieldSet, subObj, type, isFirstFloor);
219 if (ret != E_OK) {
220 break;
221 }
222 subObj = subObj->next;
223 }
224 return ret;
225 }
226
IsFieldNameLegal(const std::string & fieldName)227 bool IsFieldNameLegal(const std::string &fieldName)
228 {
229 for (auto oneChar : fieldName) {
230 if (!((isalpha(oneChar)) || (isdigit(oneChar)) || (oneChar == '_'))) {
231 return false;
232 }
233 }
234 return true;
235 }
236
CheckSubObj(std::set<std::string> & fieldSet,cJSON * subObj,int parentType,bool isFirstFloor)237 int JsonObject::CheckSubObj(std::set<std::string> &fieldSet, cJSON *subObj, int parentType, bool isFirstFloor)
238 {
239 if (subObj == nullptr) {
240 return -E_INVALID_ARGS;
241 }
242 std::string fieldName;
243 if (subObj->string != nullptr) {
244 fieldName = subObj->string;
245 if (!isFirstFloor) {
246 if (!IsFieldNameLegal(fieldName)) {
247 return -E_INVALID_ARGS;
248 }
249 }
250 if (!fieldName.empty() && isdigit(fieldName[0])) {
251 return -E_INVALID_ARGS;
252 }
253 }
254 isFirstFloor = false;
255 if (parentType == cJSON_Array) {
256 return CheckJsonRepeatField(subObj, isFirstFloor);
257 }
258 if (fieldName.empty()) {
259 return -E_INVALID_JSON_FORMAT;
260 }
261 if (fieldSet.find(fieldName) == fieldSet.end()) {
262 fieldSet.insert(fieldName);
263 } else {
264 return -E_INVALID_JSON_FORMAT;
265 }
266 return CheckJsonRepeatField(subObj, isFirstFloor);
267 }
268
Print() const269 std::string JsonObject::Print() const
270 {
271 if (cjson_ == nullptr) {
272 return "";
273 }
274 char *ret = cJSON_PrintUnformatted(cjson_);
275 std::string str = (ret == nullptr ? "" : ret);
276 cJSON_free(ret);
277 return str;
278 }
279
GetObjectItem(const std::string & field,int & errCode)280 JsonObject JsonObject::GetObjectItem(const std::string &field, int &errCode)
281 {
282 if (cjson_ == nullptr || cjson_->type != cJSON_Object) {
283 errCode = -E_INVALID_ARGS;
284 return JsonObject();
285 }
286
287 JsonObject item;
288 item.caseSensitive_ = caseSensitive_;
289 if (caseSensitive_) {
290 item.cjson_ = cJSON_GetObjectItemCaseSensitive(cjson_, field.c_str());
291 } else {
292 item.cjson_ = cJSON_GetObjectItem(cjson_, field.c_str());
293 }
294 if (item.cjson_ == nullptr) {
295 errCode = -E_NOT_FOUND;
296 }
297 return item;
298 }
299
GetNext() const300 JsonObject JsonObject::GetNext() const
301 {
302 if (cjson_ == nullptr) {
303 return JsonObject();
304 }
305 JsonObject next;
306 next.caseSensitive_ = caseSensitive_;
307 if (cjson_->next == nullptr) {
308 return JsonObject();
309 }
310 next.cjson_ = cjson_->next;
311 return next;
312 }
313
GetChild() const314 JsonObject JsonObject::GetChild() const
315 {
316 if (cjson_ == nullptr) {
317 return JsonObject();
318 }
319 JsonObject child;
320 child.caseSensitive_ = caseSensitive_;
321 if (cjson_->child == nullptr) {
322 return JsonObject();
323 }
324 child.cjson_ = cjson_->child;
325 return child;
326 }
327
DeleteItemFromObject(const std::string & field)328 int JsonObject::DeleteItemFromObject(const std::string &field)
329 {
330 if (field.empty()) {
331 return E_OK;
332 }
333 cJSON_DeleteItemFromObjectCaseSensitive(cjson_, field.c_str());
334 return E_OK;
335 }
336
AddItemToObject(const std::string & fieldName,const JsonObject & item)337 int JsonObject::AddItemToObject(const std::string &fieldName, const JsonObject &item)
338 {
339 if (cjson_ == nullptr) {
340 return -E_ERROR;
341 }
342
343 if (item.IsNull()) {
344 GLOGD("Add null object.");
345 return E_OK;
346 }
347 if (cjson_->type == cJSON_Array) {
348 int n = 0;
349 cJSON *child = cjson_->child;
350 while (child != nullptr) {
351 child = child->next;
352 n++;
353 }
354 if (IsNumber(fieldName) && n <= std::stoi(fieldName)) {
355 GLOGE("Add item object to array over size.");
356 return -E_NO_DATA;
357 }
358 }
359 if (cjson_->type != cJSON_Object) {
360 GLOGE("type conflict.");
361 return -E_DATA_CONFLICT;
362 }
363 cJSON *cpoyItem = cJSON_Duplicate(item.cjson_, true);
364 cJSON_AddItemToObject(cjson_, fieldName.c_str(), cpoyItem);
365 return E_OK;
366 }
367
AddItemToObject(const std::string & fieldName)368 int JsonObject::AddItemToObject(const std::string &fieldName)
369 {
370 if (cjson_->type == cJSON_Array) {
371 int n = 0;
372 cJSON *child = cjson_->child;
373 while (child != nullptr) {
374 child = child->next;
375 n++;
376 }
377 if (IsNumber(fieldName) && n <= std::stoi(fieldName)) {
378 GLOGE("Add item object to array over size.");
379 return -E_NO_DATA;
380 }
381 }
382 if (cjson_->type != cJSON_Object) {
383 GLOGE("type conflict.");
384 return -E_DATA_CONFLICT;
385 }
386 cJSON *emptyitem = cJSON_CreateObject();
387 cJSON_AddItemToObject(cjson_, fieldName.c_str(), emptyitem);
388 return E_OK;
389 }
390
GetItemValue() const391 ValueObject JsonObject::GetItemValue() const
392 {
393 if (cjson_ == nullptr) {
394 return ValueObject();
395 }
396 ValueObject value;
397 switch (cjson_->type) {
398 case cJSON_False:
399 case cJSON_True:
400 return ValueObject(cjson_->type == cJSON_True);
401 case cJSON_NULL:
402 return ValueObject();
403 case cJSON_Number:
404 return ValueObject(cjson_->valuedouble);
405 case cJSON_String:
406 return ValueObject(cjson_->valuestring);
407 case cJSON_Array:
408 case cJSON_Object:
409 default:
410 GLOGW("Invalid json type: %d", cjson_->type);
411 break;
412 }
413
414 return value;
415 }
416
ReplaceItemInObject(const std::string & fieldName,const JsonObject & newItem,int & errCode)417 void JsonObject::ReplaceItemInObject(const std::string &fieldName, const JsonObject &newItem, int &errCode)
418 {
419 if (!newItem.IsNull() || !this->IsNull()) {
420 if (this->GetType() == JsonObject::Type::JSON_OBJECT) {
421 if (!(this->GetObjectItem(fieldName.c_str(), errCode).IsNull())) {
422 cJSON *copyItem = cJSON_Duplicate(newItem.cjson_, true);
423 cJSON_ReplaceItemInObjectCaseSensitive(this->cjson_, fieldName.c_str(), copyItem);
424 } else {
425 cJSON *copyItem = cJSON_Duplicate(newItem.cjson_, true);
426 cJSON_AddItemToObject(this->cjson_, fieldName.c_str(), copyItem);
427 }
428 }
429 }
430 }
431
ReplaceItemInArray(const int & index,const JsonObject & newItem,int & errCode)432 void JsonObject::ReplaceItemInArray(const int &index, const JsonObject &newItem, int &errCode)
433 {
434 if (!newItem.IsNull() || !this->IsNull()) {
435 if (this->GetType() == JsonObject::Type::JSON_ARRAY) {
436 cJSON *copyItem = cJSON_Duplicate(newItem.cjson_, true);
437 cJSON_ReplaceItemInArray(this->cjson_, index, copyItem);
438 }
439 }
440 }
441
InsertItemObject(int which,const JsonObject & newItem)442 int JsonObject::InsertItemObject(int which, const JsonObject &newItem)
443 {
444 if (cjson_ == nullptr) {
445 return E_OK;
446 }
447 if (newItem.IsNull()) {
448 GLOGD("Add null object.");
449 return E_OK;
450 }
451 cJSON *cpoyItem = cJSON_Duplicate(newItem.cjson_, true);
452 cJSON_InsertItemInArray(cjson_, which, cpoyItem);
453 return E_OK;
454 }
455
GetItemField() const456 std::string JsonObject::GetItemField() const
457 {
458 if (cjson_ == nullptr) {
459 return "";
460 }
461
462 if (cjson_->string == nullptr) {
463 cJSON *tail = cjson_;
464 while (tail->next != nullptr) {
465 tail = tail->next;
466 }
467
468 int index = 0;
469 cJSON *head = cjson_;
470 while (head->prev != tail) {
471 head = head->prev;
472 index++;
473 }
474 return std::to_string(index);
475 } else {
476 return cjson_->string;
477 }
478 }
479
GetItemField(int & errCode) const480 std::string JsonObject::GetItemField(int &errCode) const
481 {
482 if (cjson_ == nullptr || cjson_->string == nullptr) {
483 errCode = E_INVALID_ARGS;
484 return "";
485 }
486 errCode = E_OK;
487 return cjson_->string;
488 }
489
GetChild(cJSON * cjson,const std::string & field,bool caseSens)490 cJSON *GetChild(cJSON *cjson, const std::string &field, bool caseSens)
491 {
492 if (cjson->type == cJSON_Object) {
493 if (caseSens) {
494 return cJSON_GetObjectItemCaseSensitive(cjson, field.c_str());
495 } else {
496 return cJSON_GetObjectItem(cjson, field.c_str());
497 }
498 } else if (cjson->type == cJSON_Array) {
499 if (!IsNumber(field)) {
500 GLOGW("Invalid json field path, expect array index.");
501 return nullptr;
502 }
503 return cJSON_GetArrayItem(cjson, std::stoi(field));
504 }
505
506 GLOGW("Invalid json field type, expect object or array.");
507 return nullptr;
508 }
509
GetChildPowerMode(cJSON * cjson,const std::string & field,bool caseSens)510 cJSON *GetChildPowerMode(cJSON *cjson, const std::string &field, bool caseSens)
511 {
512 if (cjson->type != cJSON_Object && cjson->type != cJSON_Array) {
513 GLOGW("Invalid json field type, expect object or array.");
514 return nullptr;
515 } else if (cjson->type == cJSON_Object) {
516 if (caseSens) {
517 return cJSON_GetObjectItemCaseSensitive(cjson, field.c_str());
518 } else {
519 return cJSON_GetObjectItem(cjson, field.c_str());
520 }
521 }
522
523 // type is cJSON_Array
524 if (!IsNumber(field)) {
525 cjson = cjson->child;
526 while (cjson != nullptr) {
527 cJSON *resultItem = GetChild(cjson, field, caseSens);
528 if (resultItem != nullptr) {
529 return resultItem;
530 }
531 cjson = cjson->next;
532 }
533 return nullptr;
534 }
535 return cJSON_GetArrayItem(cjson, std::stoi(field));
536 }
537
MoveToPath(cJSON * cjson,const JsonFieldPath & jsonPath,bool caseSens)538 cJSON *MoveToPath(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens)
539 {
540 for (const auto &field : jsonPath) {
541 cjson = GetChild(cjson, field, caseSens);
542 if (cjson == nullptr) {
543 break;
544 }
545 }
546 return cjson;
547 }
548
MoveToPathPowerMode(cJSON * cjson,const JsonFieldPath & jsonPath,bool caseSens)549 cJSON *MoveToPathPowerMode(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens)
550 {
551 for (const auto &field : jsonPath) {
552 cjson = GetChildPowerMode(cjson, field, caseSens);
553 if (cjson == nullptr) {
554 break;
555 }
556 }
557 return cjson;
558 }
559
IsFieldExists(const JsonFieldPath & jsonPath) const560 bool JsonObject::IsFieldExists(const JsonFieldPath &jsonPath) const
561 {
562 return (MoveToPath(cjson_, jsonPath, caseSensitive_) != nullptr);
563 }
564
IsFieldExistsPowerMode(const JsonFieldPath & jsonPath) const565 bool JsonObject::IsFieldExistsPowerMode(const JsonFieldPath &jsonPath) const
566 {
567 return (MoveToPathPowerMode(cjson_, jsonPath, caseSensitive_) != nullptr);
568 }
569
FindItem(const JsonFieldPath & jsonPath,int & errCode) const570 JsonObject JsonObject::FindItem(const JsonFieldPath &jsonPath, int &errCode) const
571 {
572 if (jsonPath.empty()) {
573 JsonObject curr = JsonObject();
574 curr.cjson_ = cjson_;
575 curr.caseSensitive_ = caseSensitive_;
576 curr.isOwner_ = false;
577 return curr;
578 }
579
580 cJSON *findItem = MoveToPath(cjson_, jsonPath, caseSensitive_);
581 if (findItem == nullptr) {
582 GLOGE("Find item failed. json field path not found.");
583 errCode = -E_JSON_PATH_NOT_EXISTS;
584 return {};
585 }
586
587 JsonObject item;
588 item.caseSensitive_ = caseSensitive_;
589 item.cjson_ = findItem;
590 return item;
591 }
592
593 // Compared with the non-powerMode mode, the node found by this function is an Array, and target is an object,
594 // if the Array contains the same object as the target, it can match this object in this mode.
FindItemPowerMode(const JsonFieldPath & jsonPath,int & errCode) const595 JsonObject JsonObject::FindItemPowerMode(const JsonFieldPath &jsonPath, int &errCode) const
596 {
597 if (jsonPath.empty()) {
598 JsonObject curr = JsonObject();
599 curr.cjson_ = cjson_;
600 curr.caseSensitive_ = caseSensitive_;
601 curr.isOwner_ = false;
602 return curr;
603 }
604
605 cJSON *findItem = MoveToPathPowerMode(cjson_, jsonPath, caseSensitive_);
606 if (findItem == nullptr) {
607 GLOGE("Find item failed. json field path not found.");
608 errCode = -E_JSON_PATH_NOT_EXISTS;
609 return {};
610 }
611 JsonObject item;
612 item.caseSensitive_ = caseSensitive_;
613 item.cjson_ = findItem;
614 return item;
615 }
616
GetObjectByPath(const JsonFieldPath & jsonPath,int & errCode) const617 ValueObject JsonObject::GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const
618 {
619 JsonObject objGot = FindItem(jsonPath, errCode);
620 if (errCode != E_OK) {
621 GLOGE("Get json value object failed. %d", errCode);
622 return {};
623 }
624 return objGot.GetItemValue();
625 }
626
DeleteItemDeeplyOnTarget(const JsonFieldPath & path)627 int JsonObject::DeleteItemDeeplyOnTarget(const JsonFieldPath &path)
628 {
629 if (path.empty()) {
630 return -E_INVALID_ARGS;
631 }
632
633 std::string fieldName = path.back();
634 JsonFieldPath patherPath = path;
635 patherPath.pop_back();
636
637 cJSON *nodeFather = MoveToPath(cjson_, patherPath, caseSensitive_);
638 if (nodeFather == nullptr) {
639 return -E_JSON_PATH_NOT_EXISTS;
640 }
641
642 if (nodeFather->type == cJSON_Object) {
643 if (caseSensitive_) {
644 cJSON_DeleteItemFromObjectCaseSensitive(nodeFather, fieldName.c_str());
645 if (nodeFather->child == nullptr && path.size() > 1) {
646 JsonFieldPath fatherPath(path.begin(), path.end() - 1);
647 DeleteItemDeeplyOnTarget(fatherPath);
648 }
649 } else {
650 cJSON_DeleteItemFromObject(nodeFather, fieldName.c_str());
651 if (nodeFather->child == nullptr && path.size() > 1) {
652 JsonFieldPath fatherPath(path.begin(), path.end() - 1);
653 DeleteItemDeeplyOnTarget(fatherPath);
654 }
655 }
656 } else if (nodeFather->type == cJSON_Array) {
657 if (!IsNumber(fieldName)) {
658 GLOGW("Invalid json field path, expect array index.");
659 return -E_JSON_PATH_NOT_EXISTS;
660 }
661 cJSON_DeleteItemFromArray(nodeFather, std::stoi(fieldName));
662 if (nodeFather->child == nullptr && path.size() > 1) {
663 JsonFieldPath fatherPath(path.begin(), path.end() - 1);
664 DeleteItemDeeplyOnTarget(fatherPath);
665 }
666 }
667
668 return E_OK;
669 }
670 #else
671 ValueObject::ValueObject(bool val)
672 {
673 valueType = ValueType::VALUE_BOOL;
674 boolValue = val;
675 }
676
677 ValueObject::ValueObject(double val)
678 {
679 valueType = ValueType::VALUE_NUMBER;
680 doubleValue = val;
681 }
682
683 ValueObject::ValueObject(const char *val)
684 {
685 valueType = ValueType::VALUE_STRING;
686 stringValue = val;
687 }
688
689 ValueObject::ValueType ValueObject::GetValueType() const
690 {
691 return valueType;
692 }
693
694 bool ValueObject::GetBoolValue() const
695 {
696 return boolValue;
697 }
698
699 int64_t ValueObject::GetIntValue() const
700 {
701 return static_cast<int64_t>(std::llround(doubleValue));
702 }
703
704 double ValueObject::GetDoubleValue() const
705 {
706 return doubleValue;
707 }
708
709 std::string ValueObject::GetStringValue() const
710 {
711 return stringValue;
712 }
713
714 JsonObject JsonObject::Parse(const std::string &jsonStr, int &errCode, bool caseSensitive, bool isFilter)
715 {
716 (void)jsonStr;
717 (void)errCode;
718 (void)caseSensitive;
719 (void)isFilter;
720 return {};
721 }
722
723 JsonObject::JsonObject()
724 {
725 cjson_ = nullptr;
726 jsonDeep_ = 0;
727 isOwner_ = false;
728 caseSensitive_ = false;
729 }
730
731 JsonObject::~JsonObject()
732 {
733 }
734
735 bool JsonObject::operator==(const JsonObject &other) const
736 {
737 return true;
738 }
739
740
741 bool JsonObject::IsNull() const
742 {
743 return true;
744 }
745
746 JsonObject::Type JsonObject::GetType() const
747 {
748 return JsonObject::Type::JSON_LEAF;
749 }
750
751 int JsonObject::GetDeep()
752 {
753 return jsonDeep_;
754 }
755
756 int JsonObject::GetDeep(cJSON *cjson)
757 {
758 return jsonDeep_;
759 }
760
761 int JsonObject::CheckNumber(cJSON *item)
762 {
763 (void)item;
764 return E_OK;
765 }
766
767 int JsonObject::Init(const std::string &str, bool isFilter)
768 {
769 (void)str;
770 (void)isFilter;
771 return E_OK;
772 }
773
774 int JsonObject::CheckJsonRepeatField(cJSON *object, bool isFirstFloor)
775 {
776 (void)object;
777 (void)isFirstFloor;
778 return E_OK;
779 }
780
781 bool IsFieldNameLegal(const std::string &fieldName)
782 {
783 (void)fieldName;
784 return true;
785 }
786
787 int JsonObject::CheckSubObj(std::set<std::string> &fieldSet, cJSON *subObj, int parentType, bool isFirstFloor)
788 {
789 (void)fieldSet;
790 (void)subObj;
791 (void)parentType;
792 (void)isFirstFloor;
793 return true;
794 }
795
796 std::string JsonObject::Print() const
797 {
798 return std::string();
799 }
800
801 JsonObject JsonObject::GetObjectItem(const std::string &field, int &errCode)
802 {
803 (void)field;
804 (void)errCode;
805 return {};
806 }
807
808 JsonObject JsonObject::GetNext() const
809 {
810 return {};
811 }
812
813 JsonObject JsonObject::GetChild() const
814 {
815 return {};
816 }
817
818 int JsonObject::DeleteItemFromObject(const std::string &field)
819 {
820 (void)field;
821 return E_OK;
822 }
823
824 int JsonObject::AddItemToObject(const std::string &fieldName, const JsonObject &item)
825 {
826 (void)fieldName;
827 (void)item;
828 return E_OK;
829 }
830
831 int JsonObject::AddItemToObject(const std::string &fieldName)
832 {
833 (void)fieldName;
834 return E_OK;
835 }
836
837 ValueObject JsonObject::GetItemValue() const
838 {
839 return {};
840 }
841
842 void JsonObject::ReplaceItemInObject(const std::string &fieldName, const JsonObject &newItem, int &errCode)
843 {
844 (void)fieldName;
845 (void)newItem;
846 (void)errCode;
847 }
848
849 void JsonObject::ReplaceItemInArray(const int &index, const JsonObject &newItem, int &errCode)
850 {
851 (void)index;
852 (void)newItem;
853 (void)errCode;
854 }
855
856 int JsonObject::InsertItemObject(int which, const JsonObject &newItem)
857 {
858 (void)which;
859 (void)newItem;
860 return E_OK;
861 }
862
863 std::string JsonObject::GetItemField() const
864 {
865 return std::string();
866 }
867
868 std::string JsonObject::GetItemField(int &errCode) const
869 {
870 (void)errCode;
871 return std::string();
872 }
873
874 cJSON *GetChild(cJSON *cjson, const std::string &field, bool caseSens)
875 {
876 (void)cjson;
877 (void)field;
878 (void)caseSens;
879 return nullptr;
880 }
881
882 cJSON *GetChildPowerMode(cJSON *cjson, const std::string &field, bool caseSens)
883 {
884 (void)cjson;
885 (void)field;
886 (void)caseSens;
887 return nullptr;
888 }
889
890 cJSON *MoveToPath(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens)
891 {
892 (void)cjson;
893 (void)jsonPath;
894 (void)caseSens;
895 return nullptr;
896 }
897
898 cJSON *MoveToPathPowerMode(cJSON *cjson, const JsonFieldPath &jsonPath, bool caseSens)
899 {
900 (void)jsonPath;
901 (void)caseSens;
902 return nullptr;
903 }
904
905 bool JsonObject::IsFieldExists(const JsonFieldPath &jsonPath) const
906 {
907 (void)jsonPath;
908 return true;
909 }
910
911 bool JsonObject::IsFieldExistsPowerMode(const JsonFieldPath &jsonPath) const
912 {
913 (void)jsonPath;
914 return true;
915 }
916
917 JsonObject JsonObject::FindItem(const JsonFieldPath &jsonPath, int &errCode) const
918 {
919 (void)jsonPath;
920 (void)errCode;
921 return {};
922 }
923
924 // Compared with the non-powerMode mode, the node found by this function is an Array, and target is an object,
925 // if the Array contains the same object as the target, it can match this object in this mode.
926 JsonObject JsonObject::FindItemPowerMode(const JsonFieldPath &jsonPath, int &errCode) const
927 {
928 (void)jsonPath;
929 (void)errCode;
930 return {};
931 }
932
933 ValueObject JsonObject::GetObjectByPath(const JsonFieldPath &jsonPath, int &errCode) const
934 {
935 (void)jsonPath;
936 (void)errCode;
937 return {};
938 }
939
940 int JsonObject::DeleteItemDeeplyOnTarget(const JsonFieldPath &path)
941 {
942 (void)path;
943 return E_OK;
944 }
945 #endif
946 } // namespace DocumentDB
947