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