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