1 /*
2  * Copyright (c) 2021 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 "json_object.h"
17 
18 #include <algorithm>
19 #include <cmath>
20 #include <queue>
21 
22 #include "db_errno.h"
23 #include "log_print.h"
24 
25 namespace DistributedDB {
26 #ifndef OMIT_JSON
27 namespace {
28     const uint32_t MAX_NEST_DEPTH = 100;
29 #ifdef JSONCPP_USE_BUILDER
30     const int JSON_VALUE_PRECISION = 16;
31     const std::string JSON_CONFIG_INDENTATION = "indentation";
32     const std::string JSON_CONFIG_COLLECT_COMMENTS = "collectComments";
33     const std::string JSON_CONFIG_PRECISION = "precision";
34     const std::string JSON_CONFIG_REJECT_DUP_KEYS = "rejectDupKeys";
35 #endif
36 }
37 uint32_t JsonObject::maxNestDepth_ = MAX_NEST_DEPTH;
38 
SetMaxNestDepth(uint32_t nestDepth)39 uint32_t JsonObject::SetMaxNestDepth(uint32_t nestDepth)
40 {
41     uint32_t preValue = maxNestDepth_;
42     // No need to check the reasonability, only test code will use this method
43     maxNestDepth_ = nestDepth;
44     return preValue;
45 }
46 
CalculateNestDepth(const std::string & inString,int & errCode)47 uint32_t JsonObject::CalculateNestDepth(const std::string &inString, int &errCode)
48 {
49     auto begin = reinterpret_cast<const uint8_t *>(inString.c_str());
50     auto end = begin + inString.size();
51     return CalculateNestDepth(begin, end, errCode);
52 }
53 
CalculateNestDepth(const uint8_t * dataBegin,const uint8_t * dataEnd,int & errCode)54 uint32_t JsonObject::CalculateNestDepth(const uint8_t *dataBegin, const uint8_t *dataEnd, int &errCode)
55 {
56     if (dataBegin == nullptr || dataEnd == nullptr || dataBegin >= dataEnd) {
57         errCode = -E_INVALID_ARGS;
58         return maxNestDepth_ + 1; // return a invalid depth
59     }
60     bool isInString = false;
61     uint32_t maxDepth = 0;
62     uint32_t objectDepth = 0;
63     uint32_t arrayDepth = 0;
64     uint32_t numOfEscape = 0;
65 
66     for (auto ptr = dataBegin; ptr < dataEnd; ptr++) {
67         if (*ptr == '"' && numOfEscape % 2 == 0) { // 2 used to detect parity
68             isInString = !isInString;
69             continue;
70         }
71         if (!isInString) {
72             if (*ptr == '{') {
73                 objectDepth++;
74                 maxDepth = std::max(maxDepth, objectDepth + arrayDepth);
75             }
76             if (*ptr == '}') {
77                 objectDepth = ((objectDepth > 0) ? (objectDepth - 1) : 0);
78             }
79             if (*ptr == '[') {
80                 arrayDepth++;
81                 maxDepth = std::max(maxDepth, objectDepth + arrayDepth);
82             }
83             if (*ptr == ']') {
84                 arrayDepth = ((arrayDepth > 0) ? (arrayDepth - 1) : 0);
85             }
86         }
87         numOfEscape = ((*ptr == '\\') ? (numOfEscape + 1) : 0);
88     }
89     return maxDepth;
90 }
91 
JsonObject(const JsonObject & other)92 JsonObject::JsonObject(const JsonObject &other)
93 {
94     isValid_ = other.isValid_;
95     value_ = other.value_;
96 }
97 
operator =(const JsonObject & other)98 JsonObject& JsonObject::operator=(const JsonObject &other)
99 {
100     if (&other != this) {
101         isValid_ = other.isValid_;
102         value_ = other.value_;
103     }
104     return *this;
105 }
106 
JsonObject(const Json::Value & value)107 JsonObject::JsonObject(const Json::Value &value) : isValid_(true), value_(value)
108 {
109 }
110 
Parse(const std::string & inString)111 int JsonObject::Parse(const std::string &inString)
112 {
113     // The jsoncpp lib parser in strict mode will still regard root type jsonarray as valid, but we require jsonobject
114     if (isValid_) {
115         LOGE("[Json][Parse] Already Valid.");
116         return -E_NOT_PERMIT;
117     }
118     int errCode = E_OK;
119     uint32_t nestDepth = CalculateNestDepth(inString, errCode);
120     if (errCode != E_OK || nestDepth > maxNestDepth_) {
121         LOGE("[Json][Parse] Json calculate nest depth failed %d, depth=%" PRIu32 " exceed max allowed:%" PRIu32,
122             errCode, nestDepth, maxNestDepth_);
123         return -E_JSON_PARSE_FAIL;
124     }
125 #ifdef JSONCPP_USE_BUILDER
126     JSONCPP_STRING errs;
127     Json::CharReaderBuilder builder;
128     Json::CharReaderBuilder::strictMode(&builder.settings_);
129     builder[JSON_CONFIG_COLLECT_COMMENTS] = false;
130     builder[JSON_CONFIG_REJECT_DUP_KEYS] = false;
131     std::unique_ptr<Json::CharReader> const jsonReader(builder.newCharReader());
132 
133     auto begin = reinterpret_cast<const std::string::value_type *>(inString.c_str());
134     auto end = reinterpret_cast<const std::string::value_type *>(inString.c_str() + inString.length());
135     if (!jsonReader->parse(begin, end, &value_, &errs)) {
136         value_ = Json::Value();
137         LOGE("[Json][Parse] Parse string to JsonValue fail, reason=%s.", errs.c_str());
138         return -E_JSON_PARSE_FAIL;
139     }
140 #else
141     Json::Reader reader(Json::Features::strictMode());
142     if (!reader.parse(inString, value_, false)) {
143         value_ = Json::Value();
144         LOGE("[Json][Parse] Parse string to JsonValue fail, reason=%s.", reader.getFormattedErrorMessages().c_str());
145         return -E_JSON_PARSE_FAIL;
146     }
147 #endif
148     // The jsoncpp lib parser in strict mode will still regard root type jsonarray as valid, but we require jsonobject
149     if (value_.type() != Json::ValueType::objectValue) {
150         value_ = Json::Value();
151         LOGE("[Json][Parse] Not an object at root.");
152         return -E_JSON_PARSE_FAIL;
153     }
154     isValid_ = true;
155     return E_OK;
156 }
157 
Parse(const std::vector<uint8_t> & inData)158 int JsonObject::Parse(const std::vector<uint8_t> &inData)
159 {
160     if (inData.empty()) {
161         return -E_INVALID_ARGS;
162     }
163     return Parse(inData.data(), inData.data() + inData.size());
164 }
165 
Parse(const uint8_t * dataBegin,const uint8_t * dataEnd)166 int JsonObject::Parse(const uint8_t *dataBegin, const uint8_t *dataEnd)
167 {
168     if (isValid_) {
169         LOGE("[Json][Parse] Already Valid.");
170         return -E_NOT_PERMIT;
171     }
172     if (dataBegin == nullptr || dataEnd == nullptr || dataBegin >= dataEnd) {
173         return -E_INVALID_ARGS;
174     }
175     int errCode = E_OK;
176     uint32_t nestDepth = CalculateNestDepth(dataBegin, dataEnd, errCode);
177     if (errCode != E_OK || nestDepth > maxNestDepth_) {
178         LOGE("[Json][Parse] Json calculate nest depth failed %d, depth:%" PRIu32 " exceed max allowed:%" PRIu32,
179             errCode, nestDepth, maxNestDepth_);
180         return -E_JSON_PARSE_FAIL;
181     }
182 #ifdef JSONCPP_USE_BUILDER
183     std::string jsonStr(dataBegin, dataEnd);
184     auto begin = jsonStr.c_str();
185     auto end = jsonStr.c_str() + jsonStr.size();
186 
187     JSONCPP_STRING errs;
188     Json::CharReaderBuilder builder;
189     Json::CharReaderBuilder::strictMode(&builder.settings_);
190     builder[JSON_CONFIG_COLLECT_COMMENTS] = false;
191     builder[JSON_CONFIG_REJECT_DUP_KEYS] = false;
192     std::unique_ptr<Json::CharReader> const jsonReader(builder.newCharReader());
193     // The endDoc parameter of reader::parse refer to the byte after the string itself
194     if (!jsonReader->parse(begin, end, &value_, &errs)) {
195         value_ = Json::Value();
196         LOGE("[Json][Parse] Parse dataRange to JsonValue fail, reason=%s.", errs.c_str());
197         return -E_JSON_PARSE_FAIL;
198     }
199 #else
200     Json::Reader reader(Json::Features::strictMode());
201     auto begin = reinterpret_cast<const std::string::value_type *>(dataBegin);
202     auto end = reinterpret_cast<const std::string::value_type *>(dataEnd);
203     // The endDoc parameter of reader::parse refer to the byte after the string itself
204     if (!reader.parse(begin, end, value_, false)) {
205         value_ = Json::Value();
206         LOGE("[Json][Parse] Parse dataRange to JsonValue fail, reason=%s.", reader.getFormattedErrorMessages().c_str());
207         return -E_JSON_PARSE_FAIL;
208     }
209 #endif
210     // The jsoncpp lib parser in strict mode will still regard root type jsonarray as valid, but we require jsonobject
211     if (value_.type() != Json::ValueType::objectValue) {
212         value_ = Json::Value();
213         LOGE("[Json][Parse] Not an object at root.");
214         return -E_JSON_PARSE_FAIL;
215     }
216     isValid_ = true;
217     return E_OK;
218 }
219 
IsValid() const220 bool JsonObject::IsValid() const
221 {
222     return isValid_;
223 }
224 
ToString() const225 std::string JsonObject::ToString() const
226 {
227     if (!isValid_) {
228         LOGE("[Json][ToString] Not Valid Yet.");
229         return std::string();
230     }
231 #ifdef JSONCPP_USE_BUILDER
232     Json::StreamWriterBuilder writerBuilder;
233     writerBuilder[JSON_CONFIG_INDENTATION] = "";
234     writerBuilder[JSON_CONFIG_PRECISION] = JSON_VALUE_PRECISION;
235     std::unique_ptr<Json::StreamWriter> const jsonWriter(writerBuilder.newStreamWriter());
236     std::stringstream ss;
237     jsonWriter->write(value_, &ss);
238     // The endingLineFeedSymbol is left empty by default.
239     return ss.str();
240 #else
241     Json::FastWriter fastWriter;
242     // Call omitEndingLineFeed to let JsonCpp not append an \n at the end of string. If not doing so, when passing a
243     // minified jsonString, the result of this function will be one byte longer then the original, which may cause the
244     // result checked as length invalid by upper logic when the original length is just at the limitation boundary.
245     fastWriter.omitEndingLineFeed();
246     return fastWriter.write(value_);
247 #endif
248 }
249 
IsFieldPathExist(const FieldPath & inPath) const250 bool JsonObject::IsFieldPathExist(const FieldPath &inPath) const
251 {
252     if (!isValid_) {
253         LOGE("[Json][isExisted] Not Valid Yet.");
254         return false;
255     }
256     int errCode = E_OK;
257     (void)GetJsonValueByFieldPath(inPath, errCode); // Ignore return const reference
258     return (errCode == E_OK);
259 }
260 
GetFieldTypeByFieldPath(const FieldPath & inPath,FieldType & outType) const261 int JsonObject::GetFieldTypeByFieldPath(const FieldPath &inPath, FieldType &outType) const
262 {
263     if (!isValid_) {
264         LOGE("[Json][GetType] Not Valid Yet.");
265         return -E_NOT_PERMIT;
266     }
267     int errCode = E_OK;
268     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
269     if (errCode != E_OK) {
270         return errCode;
271     }
272     return GetFieldTypeByJsonValue(valueNode, outType);
273 }
274 
GetFieldValueByFieldPath(const FieldPath & inPath,FieldValue & outValue) const275 int JsonObject::GetFieldValueByFieldPath(const FieldPath &inPath, FieldValue &outValue) const
276 {
277     if (!isValid_) {
278         LOGE("[Json][GetValue] Not Valid Yet.");
279         return -E_NOT_PERMIT;
280     }
281     int errCode = E_OK;
282     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
283     if (errCode != E_OK) {
284         return errCode;
285     }
286     FieldType valueType;
287     errCode = GetFieldTypeByJsonValue(valueNode, valueType);
288     if (errCode != E_OK) {
289         return errCode;
290     }
291     switch (valueType) {
292         case FieldType::LEAF_FIELD_BOOL:
293             outValue.boolValue = valueNode.asBool();
294             break;
295         case FieldType::LEAF_FIELD_INTEGER:
296             outValue.integerValue = valueNode.asInt();
297             break;
298         case FieldType::LEAF_FIELD_LONG:
299             outValue.longValue = valueNode.asInt64();
300             break;
301         case FieldType::LEAF_FIELD_DOUBLE:
302             outValue.doubleValue = valueNode.asDouble();
303             break;
304         case FieldType::LEAF_FIELD_STRING:
305             outValue.stringValue = valueNode.asString();
306             break;
307         default:
308             return -E_NOT_SUPPORT;
309     }
310     return E_OK;
311 }
312 
GetSubFieldPath(const FieldPath & inPath,std::set<FieldPath> & outSubPath) const313 int JsonObject::GetSubFieldPath(const FieldPath &inPath, std::set<FieldPath> &outSubPath) const
314 {
315     if (!isValid_) {
316         LOGE("[Json][GetSubPath] Not Valid Yet.");
317         return -E_NOT_PERMIT;
318     }
319     int errCode = E_OK;
320     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
321     if (errCode != E_OK) {
322         return errCode;
323     }
324     if (valueNode.type() != Json::ValueType::objectValue) {
325         return -E_NOT_SUPPORT;
326     }
327     // Note: the subFields JsonCpp returnout will be different from each other
328     std::vector<std::string> subFields = valueNode.getMemberNames();
329     for (const auto &eachSubField : subFields) {
330         FieldPath eachSubPath = inPath;
331         eachSubPath.emplace_back(eachSubField);
332         outSubPath.insert(eachSubPath);
333     }
334     return E_OK;
335 }
336 
GetSubFieldPath(const std::set<FieldPath> & inPath,std::set<FieldPath> & outSubPath) const337 int JsonObject::GetSubFieldPath(const std::set<FieldPath> &inPath, std::set<FieldPath> &outSubPath) const
338 {
339     for (const auto &eachPath : inPath) {
340         int errCode = GetSubFieldPath(eachPath, outSubPath);
341         if (errCode != E_OK) {
342             return errCode;
343         }
344     }
345     return E_OK;
346 }
347 
GetSubFieldPathAndType(const FieldPath & inPath,std::map<FieldPath,FieldType> & outSubPathType) const348 int JsonObject::GetSubFieldPathAndType(const FieldPath &inPath, std::map<FieldPath, FieldType> &outSubPathType) const
349 {
350     if (!isValid_) {
351         LOGE("[Json][GetSubPathType] Not Valid Yet.");
352         return -E_NOT_PERMIT;
353     }
354     int errCode = E_OK;
355     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
356     if (errCode != E_OK) {
357         return errCode;
358     }
359     if (valueNode.type() != Json::ValueType::objectValue) {
360         return -E_NOT_SUPPORT;
361     }
362     // Note: the subFields JsonCpp returnout will be different from each other
363     std::vector<std::string> subFields = valueNode.getMemberNames();
364     for (const auto &eachSubField : subFields) {
365         FieldPath eachSubPath = inPath;
366         eachSubPath.push_back(eachSubField);
367         FieldType eachSubType;
368         errCode = GetFieldTypeByJsonValue(valueNode[eachSubField], eachSubType);
369         if (errCode != E_OK) {
370             return errCode;
371         }
372         outSubPathType[eachSubPath] = eachSubType;
373     }
374     return E_OK;
375 }
376 
GetSubFieldPathAndType(const std::set<FieldPath> & inPath,std::map<FieldPath,FieldType> & outSubPathType) const377 int JsonObject::GetSubFieldPathAndType(const std::set<FieldPath> &inPath,
378     std::map<FieldPath, FieldType> &outSubPathType) const
379 {
380     for (const auto &eachPath : inPath) {
381         int errCode = GetSubFieldPathAndType(eachPath, outSubPathType);
382         if (errCode != E_OK) {
383             return errCode;
384         }
385     }
386     return E_OK;
387 }
388 
GetArraySize(const FieldPath & inPath,uint32_t & outSize) const389 int JsonObject::GetArraySize(const FieldPath &inPath, uint32_t &outSize) const
390 {
391     if (!isValid_) {
392         LOGE("[Json][GetArraySize] Not Valid Yet.");
393         return -E_NOT_PERMIT;
394     }
395     int errCode = E_OK;
396     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
397     if (errCode != E_OK) {
398         return errCode;
399     }
400     if (valueNode.type() != Json::ValueType::arrayValue) {
401         return -E_NOT_SUPPORT;
402     }
403     outSize = valueNode.size();
404     return E_OK;
405 }
406 
GetArrayContentOfStringOrStringArray(const FieldPath & inPath,std::vector<std::vector<std::string>> & outContent) const407 int JsonObject::GetArrayContentOfStringOrStringArray(const FieldPath &inPath,
408     std::vector<std::vector<std::string>> &outContent) const
409 {
410     if (!isValid_) {
411         LOGE("[Json][GetArrayContent] Not Valid Yet.");
412         return -E_NOT_PERMIT;
413     }
414     int errCode = E_OK;
415     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
416     if (errCode != E_OK) {
417         LOGW("[Json][GetArrayContent] Get JsonValue Fail=%d.", errCode);
418         return errCode;
419     }
420     if (valueNode.type() != Json::ValueType::arrayValue) {
421         LOGE("[Json][GetArrayContent] Not an array.");
422         return -E_NOT_SUPPORT;
423     }
424     for (uint32_t index = 0; index < valueNode.size(); index++) {
425         const Json::Value &eachArrayItem = valueNode[index];
426         if (eachArrayItem.isString()) {
427             outContent.emplace_back(std::vector<std::string>({eachArrayItem.asString()}));
428             continue;
429         }
430         if (eachArrayItem.isArray()) {
431             if (eachArrayItem.empty()) {
432                 continue; // Ignore empty array-type member
433             }
434             outContent.emplace_back(std::vector<std::string>());
435             errCode = GetStringArrayContentByJsonValue(eachArrayItem, outContent.back());
436             if (errCode == E_OK) {
437                 continue; // Everything ok
438             }
439         }
440         // If reach here, then something is not ok
441         outContent.clear();
442         LOGE("[Json][GetArrayContent] Not string or array fail=%d at index:%" PRIu32, errCode, index);
443         return -E_NOT_SUPPORT;
444     }
445     return E_OK;
446 }
447 
448 namespace {
InsertFieldCheckParameter(const FieldPath & inPath,FieldType inType,const FieldValue & inValue,uint32_t maxNestDepth)449 bool InsertFieldCheckParameter(const FieldPath &inPath, FieldType inType, const FieldValue &inValue,
450     uint32_t maxNestDepth)
451 {
452     if (inPath.empty() || inPath.size() > maxNestDepth || inType == FieldType::LEAF_FIELD_ARRAY ||
453         inType == FieldType::INTERNAL_FIELD_OBJECT) {
454         return false;
455     }
456     // Infinite double not support
457     return !(inType == FieldType::LEAF_FIELD_DOUBLE && !std::isfinite(inValue.doubleValue));
458 }
459 
LeafJsonNodeAppendValue(Json::Value & leafNode,FieldType inType,const FieldValue & inValue)460 void LeafJsonNodeAppendValue(Json::Value &leafNode, FieldType inType, const FieldValue &inValue)
461 {
462     if (inType == FieldType::LEAF_FIELD_STRING) {
463         leafNode.append(Json::Value(inValue.stringValue));
464     }
465 }
466 
467 // Function design for InsertField call on an null-type Json::Value
LeafJsonNodeAssignValue(Json::Value & leafNode,FieldType inType,const FieldValue & inValue)468 void LeafJsonNodeAssignValue(Json::Value &leafNode, FieldType inType, const FieldValue &inValue)
469 {
470     switch (inType) {
471         case FieldType::LEAF_FIELD_BOOL:
472             leafNode = Json::Value(inValue.boolValue);
473             break;
474         case FieldType::LEAF_FIELD_INTEGER:
475             // Cast to Json::Int to avoid "ambiguous call of overloaded function"
476             leafNode = Json::Value(static_cast<Json::Int>(inValue.integerValue));
477             break;
478         case FieldType::LEAF_FIELD_LONG:
479             // Cast to Json::Int64 to avoid "ambiguous call of overloaded function"
480             leafNode = Json::Value(static_cast<Json::Int64>(inValue.longValue));
481             break;
482         case FieldType::LEAF_FIELD_DOUBLE:
483             leafNode = Json::Value(inValue.doubleValue);
484             break;
485         case FieldType::LEAF_FIELD_STRING:
486             leafNode = Json::Value(inValue.stringValue);
487             break;
488         case FieldType::LEAF_FIELD_OBJECT:
489             leafNode = Json::Value(Json::ValueType::objectValue);
490             break;
491         default:
492             // For LEAF_FIELD_NULL, Do nothing.
493             // For LEAF_FIELD_ARRAY and INTERNAL_FIELD_OBJECT, Not Support, had been excluded by InsertField
494             return;
495     }
496 }
497 }
498 
499 // move the nearest to the leaf of inPath, if not exist, will create it, else it should be an array object.
MoveToPath(const FieldPath & inPath,Json::Value * & exact,Json::Value * & nearest)500 int JsonObject::MoveToPath(const FieldPath &inPath, Json::Value *&exact, Json::Value *&nearest)
501 {
502     uint32_t nearDepth = 0;
503     int errCode = LocateJsonValueByFieldPath(inPath, exact, nearest, nearDepth);
504     if (errCode != -E_NOT_FOUND) { // Path already exist and it's not an array object
505         return -E_JSON_INSERT_PATH_EXIST;
506     }
507     // nearDepth 0 represent for root value. nearDepth equal to inPath.size indicate an exact path match
508     if (nearest == nullptr || nearDepth >= inPath.size()) { // Impossible
509         return -E_INTERNAL_ERROR;
510     }
511     if (nearest->type() != Json::ValueType::objectValue) { // path ends with type not object
512         return -E_JSON_INSERT_PATH_CONFLICT;
513     }
514     // Use nearDepth as startIndex pointing to the first field that lacked
515     for (uint32_t lackFieldIndex = nearDepth; lackFieldIndex < inPath.size(); lackFieldIndex++) {
516         // The new JsonValue is null-type, we can safely add members to an null-type JsonValue which will turn into
517         // object-type after member adding. Then move "nearest" to point to the new JsonValue.
518         nearest = &((*nearest)[inPath[lackFieldIndex]]);
519     }
520     return E_OK;
521 }
522 
InsertField(const FieldPath & inPath,const JsonObject & inValue,bool isAppend)523 int JsonObject::InsertField(const FieldPath &inPath, const JsonObject &inValue, bool isAppend)
524 {
525     if (inPath.empty() || inPath.size() > maxNestDepth_ || !inValue.IsValid()) {
526         return -E_INVALID_ARGS;
527     }
528     if (!isValid_) {
529         value_ = Json::Value(Json::ValueType::objectValue);
530         isValid_ = true;
531     }
532     Json::Value *exact = nullptr;
533     Json::Value *nearest = nullptr;
534     int errCode = MoveToPath(inPath, exact, nearest);
535     if (errCode != E_OK) {
536         return errCode;
537     }
538     LOGD("nearest type is %d", nearest->type());
539     if (isAppend || nearest->type() == Json::ValueType::arrayValue) {
540         nearest->append(inValue.value_);
541     } else {
542         *nearest = inValue.value_;
543     }
544     return E_OK;
545 }
546 
InsertField(const FieldPath & inPath,FieldType inType,const FieldValue & inValue,bool isAppend)547 int JsonObject::InsertField(const FieldPath &inPath, FieldType inType, const FieldValue &inValue, bool isAppend)
548 {
549     if (!InsertFieldCheckParameter(inPath, inType, inValue, maxNestDepth_)) {
550         return -E_INVALID_ARGS;
551     }
552     if (!isValid_) {
553         // Insert on invalid object never fail after parameter check ok, so here no need concern rollback.
554         value_ = Json::Value(Json::ValueType::objectValue);
555         isValid_ = true;
556     }
557     Json::Value *exact = nullptr;
558     Json::Value *nearest = nullptr;
559     int errCode = MoveToPath(inPath, exact, nearest);
560     if (errCode != E_OK) {
561         return errCode;
562     }
563     // Here "nearest" points to the JsonValue(null-type now) corresponding to the last field
564     if (isAppend || nearest->type() == Json::ValueType::arrayValue) {
565         LeafJsonNodeAppendValue(*nearest, inType, inValue);
566     } else {
567         LeafJsonNodeAssignValue(*nearest, inType, inValue);
568     }
569     return E_OK;
570 }
571 
DeleteField(const FieldPath & inPath)572 int JsonObject::DeleteField(const FieldPath &inPath)
573 {
574     if (!isValid_) {
575         LOGE("[Json][DeleteField] Not Valid Yet.");
576         return -E_NOT_PERMIT;
577     }
578     if (inPath.empty()) {
579         return -E_INVALID_ARGS;
580     }
581     Json::Value *exact = nullptr;
582     Json::Value *nearest = nullptr;
583     uint32_t nearDepth = 0;
584     int errCode = LocateJsonValueByFieldPath(inPath, exact, nearest, nearDepth);
585     if (errCode != E_OK) { // Path not exist
586         return -E_JSON_DELETE_PATH_NOT_FOUND;
587     }
588     // nearDepth should be equal to inPath.size() - 1, because nearest is at the parent path of inPath
589     if (nearest == nullptr || nearest->type() != Json::ValueType::objectValue || nearDepth != inPath.size() - 1) {
590         return -E_INTERNAL_ERROR; // Impossible
591     }
592     // Remove member from nearest, ignore returned removed Value, use nearDepth as index pointing to last field of path.
593     (void)nearest->removeMember(inPath[nearDepth]);
594     return E_OK;
595 }
596 
GetStringArrayContentByJsonValue(const Json::Value & value,std::vector<std::string> & outStringArray) const597 int JsonObject::GetStringArrayContentByJsonValue(const Json::Value &value,
598     std::vector<std::string> &outStringArray) const
599 {
600     if (value.type() != Json::ValueType::arrayValue) {
601         LOGE("[Json][GetStringArrayByValue] Not an array.");
602         return -E_NOT_SUPPORT;
603     }
604     for (uint32_t index = 0; index < value.size(); index++) {
605         const Json::Value &eachArrayItem = value[index];
606         if (!eachArrayItem.isString()) {
607             LOGE("[Json][GetStringArrayByValue] Index=%u in Array is not string.", index);
608             outStringArray.clear();
609             return -E_NOT_SUPPORT;
610         }
611         outStringArray.push_back(eachArrayItem.asString());
612     }
613     return E_OK;
614 }
615 
GetFieldTypeByJsonValue(const Json::Value & value,FieldType & outType) const616 int JsonObject::GetFieldTypeByJsonValue(const Json::Value &value, FieldType &outType) const
617 {
618     Json::ValueType valueType = value.type();
619     switch (valueType) {
620         case Json::ValueType::nullValue:
621             outType = FieldType::LEAF_FIELD_NULL;
622             break;
623         case Json::ValueType::booleanValue:
624             outType = FieldType::LEAF_FIELD_BOOL;
625             break;
626         // The case intValue and uintValue cover from INT64_MIN to UINT64_MAX. Inside this range, isInt() take range
627         // from INT32_MIN to INT32_MAX, which should be regard as LEAF_FIELD_INTEGER; isInt64() take range from
628         // INT64_MIN to INT64_MAX, which should be regard as LEAF_FIELD_LONG if it is not LEAF_FIELD_INTEGER;
629         // INT64_MAX + 1 to UINT64_MAX will be regard as LEAF_FIELD_DOUBLE, therefore lose its precision when read out
630         // as double value.
631         case Json::ValueType::intValue:
632         case Json::ValueType::uintValue:
633             if (value.isInt()) {
634                 outType = FieldType::LEAF_FIELD_INTEGER;
635             } else if (value.isInt64()) {
636                 outType = FieldType::LEAF_FIELD_LONG;
637             } else {
638                 outType = FieldType::LEAF_FIELD_DOUBLE; // The isDouble() judge is always true in this case.
639             }
640             break;
641         // Integral value beyond range INT64_MIN to UINT64_MAX will be recognized as realValue and lose its precision.
642         // Value in scientific notation or has decimal point will be recognized as realValue without exception,
643         // no matter whether the value is large or small, no matter with or without non-zero decimal part.
644         // In a word, when regard as DOUBLE type, a value can not guarantee its presision
645         case Json::ValueType::realValue:
646             // The isDouble() judge is always true in this case. A value exceed double range is not support.
647             outType = FieldType::LEAF_FIELD_DOUBLE;
648             if (!std::isfinite(value.asDouble())) {
649                 LOGE("[Json][GetTypeByJson] Infinite double not support.");
650                 return -E_NOT_SUPPORT;
651             }
652             break;
653         case Json::ValueType::stringValue:
654             outType = FieldType::LEAF_FIELD_STRING;
655             break;
656         case Json::ValueType::arrayValue:
657             outType = FieldType::LEAF_FIELD_ARRAY;
658             break;
659         case Json::ValueType::objectValue:
660             if (value.getMemberNames().empty()) {
661                 outType = FieldType::LEAF_FIELD_OBJECT;
662                 break;
663             }
664             outType = FieldType::INTERNAL_FIELD_OBJECT;
665             break;
666         default:
667             LOGE("[Json][GetTypeByJson] no such type.");
668             return -E_NOT_SUPPORT;
669     }
670     return E_OK;
671 }
672 
GetJsonValueByFieldPath(const FieldPath & inPath,int & errCode) const673 const Json::Value &JsonObject::GetJsonValueByFieldPath(const FieldPath &inPath, int &errCode) const
674 {
675     // Root path always exist
676     if (inPath.empty()) {
677         errCode = E_OK;
678         return value_;
679     }
680     const Json::Value *valueNode = &value_;
681     for (const auto &eachPathSegment : inPath) {
682         if ((valueNode->type() != Json::ValueType::objectValue) || (!valueNode->isMember(eachPathSegment))) {
683             // Current JsonValue is not an object, or no such member field
684             errCode = -E_INVALID_PATH;
685             return value_;
686         }
687         valueNode = &((*valueNode)[eachPathSegment]);
688     }
689     errCode = E_OK;
690     return *valueNode;
691 }
692 
LocateJsonValueByFieldPath(const FieldPath & inPath,Json::Value * & exact,Json::Value * & nearest,uint32_t & nearDepth)693 int JsonObject::LocateJsonValueByFieldPath(const FieldPath &inPath, Json::Value *&exact,
694     Json::Value *&nearest, uint32_t &nearDepth)
695 {
696     if (!isValid_) {
697         return -E_NOT_PERMIT;
698     }
699     exact = &value_;
700     nearest = &value_;
701     nearDepth = 0;
702     if (inPath.empty()) {
703         return E_OK;
704     }
705     for (const auto &eachPathSegment : inPath) {
706         nearest = exact; // Let "nearest" trace "exact" before "exact" go deeper
707         if (nearest != &value_) {
708             nearDepth++; // For each "nearest" trace up "exact", increase nearDepth to indicate where it is.
709         }
710         if ((exact->type() != Json::ValueType::objectValue) || (!exact->isMember(eachPathSegment))) {
711             // "exact" is not an object, or no such member field
712             exact = nullptr; // Set "exact" to nullptr indicate exact path not exist
713             return -E_NOT_FOUND;
714         }
715         exact = &((*exact)[eachPathSegment]); // "exact" go deeper
716     }
717     if (exact->type() == Json::ValueType::arrayValue) {
718         return -E_NOT_FOUND; // could append value if path is an array field.
719     }
720     // Here, JsonValue exist at exact path, "nearest" is "exact" parent.
721     return E_OK;
722 }
723 
GetObjectArrayByFieldPath(const FieldPath & inPath,std::vector<JsonObject> & outArray) const724 int JsonObject::GetObjectArrayByFieldPath(const FieldPath &inPath, std::vector<JsonObject> &outArray) const
725 {
726     if (!isValid_) {
727         LOGE("[Json][GetValue] Not Valid Yet.");
728         return -E_NOT_PERMIT;
729     }
730     int errCode = E_OK;
731     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
732     if (errCode != E_OK) {
733         LOGE("[Json][GetValue] Get json value failed. %d", errCode);
734         return errCode;
735     }
736 
737     if (!valueNode.isArray()) {
738         LOGE("[Json][GetValue] Not Array type.");
739         return -E_NOT_PERMIT;
740     }
741     for (Json::ArrayIndex i = 0; i < valueNode.size(); ++i) {
742         outArray.emplace_back(JsonObject(valueNode[i]));
743     }
744     return E_OK;
745 }
746 
GetObjectByFieldPath(const FieldPath & inPath,JsonObject & outObj) const747 int JsonObject::GetObjectByFieldPath(const FieldPath &inPath, JsonObject &outObj) const
748 {
749     if (!isValid_) {
750         LOGE("[Json][GetValue] Not Valid Yet.");
751         return -E_NOT_PERMIT;
752     }
753     int errCode = E_OK;
754     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
755     if (errCode != E_OK) {
756         LOGE("[Json][GetValue] Get json value failed. %d", errCode);
757         return errCode;
758     }
759 
760     if (!valueNode.isObject()) {
761         LOGE("[Json][GetValue] Not Object type.");
762         return -E_NOT_PERMIT;
763     }
764     outObj = JsonObject(valueNode);
765     return E_OK;
766 }
767 
GetStringArrayByFieldPath(const FieldPath & inPath,std::vector<std::string> & outArray) const768 int JsonObject::GetStringArrayByFieldPath(const FieldPath &inPath, std::vector<std::string> &outArray) const
769 {
770     if (!isValid_) {
771         LOGE("[Json][GetValue] Not Valid Yet.");
772         return -E_NOT_PERMIT;
773     }
774     int errCode = E_OK;
775     const Json::Value &valueNode = GetJsonValueByFieldPath(inPath, errCode);
776     if (errCode != E_OK) {
777         LOGE("[Json][GetValue] Get json value failed. %d", errCode);
778         return errCode;
779     }
780 
781     return GetStringArrayContentByJsonValue(valueNode, outArray);
782 }
783 
784 #else // OMIT_JSON
785 uint32_t JsonObject::SetMaxNestDepth(uint32_t nestDepth)
786 {
787     (void)nestDepth;
788     return 0;
789 }
790 
791 uint32_t JsonObject::CalculateNestDepth(const std::string &inString, int &errCode)
792 {
793     (void)inString;
794     (void)errCode;
795     return 0;
796 }
797 
798 uint32_t JsonObject::CalculateNestDepth(const uint8_t *dataBegin, const uint8_t *dataEnd, int &errCode)
799 {
800     (void)dataBegin;
801     (void)dataEnd;
802     (void)errCode;
803     return 0;
804 }
805 
806 JsonObject::JsonObject(const JsonObject &other) = default;
807 
808 JsonObject& JsonObject::operator=(const JsonObject &other) = default;
809 
810 int JsonObject::Parse(const std::string &inString)
811 {
812     (void)inString;
813     LOGW("[Json][Parse] Json Omit From Compile.");
814     return -E_NOT_PERMIT;
815 }
816 
817 int JsonObject::Parse(const std::vector<uint8_t> &inData)
818 {
819     (void)inData;
820     LOGW("[Json][Parse] Json Omit From Compile.");
821     return -E_NOT_PERMIT;
822 }
823 
824 int JsonObject::Parse(const uint8_t *dataBegin, const uint8_t *dataEnd)
825 {
826     (void)dataBegin;
827     (void)dataEnd;
828     LOGW("[Json][Parse] Json Omit From Compile.");
829     return -E_NOT_PERMIT;
830 }
831 
832 bool JsonObject::IsValid() const
833 {
834     return false;
835 }
836 
837 std::string JsonObject::ToString() const
838 {
839     return std::string();
840 }
841 
842 bool JsonObject::IsFieldPathExist(const FieldPath &inPath) const
843 {
844     (void)inPath;
845     return false;
846 }
847 
848 int JsonObject::GetFieldTypeByFieldPath(const FieldPath &inPath, FieldType &outType) const
849 {
850     (void)inPath;
851     (void)outType;
852     return -E_NOT_PERMIT;
853 }
854 
855 int JsonObject::GetFieldValueByFieldPath(const FieldPath &inPath, FieldValue &outValue) const
856 {
857     (void)inPath;
858     (void)outValue;
859     return -E_NOT_PERMIT;
860 }
861 
862 int JsonObject::GetSubFieldPath(const FieldPath &inPath, std::set<FieldPath> &outSubPath) const
863 {
864     (void)inPath;
865     (void)outSubPath;
866     return -E_NOT_PERMIT;
867 }
868 
869 int JsonObject::GetSubFieldPath(const std::set<FieldPath> &inPath, std::set<FieldPath> &outSubPath) const
870 {
871     (void)inPath;
872     (void)outSubPath;
873     return -E_NOT_PERMIT;
874 }
875 
876 int JsonObject::GetSubFieldPathAndType(const FieldPath &inPath, std::map<FieldPath, FieldType> &outSubPathType) const
877 {
878     (void)inPath;
879     (void)outSubPathType;
880     return -E_NOT_PERMIT;
881 }
882 
883 int JsonObject::GetSubFieldPathAndType(const std::set<FieldPath> &inPath,
884     std::map<FieldPath, FieldType> &outSubPathType) const
885 {
886     (void)inPath;
887     (void)outSubPathType;
888     return -E_NOT_PERMIT;
889 }
890 
891 int JsonObject::GetArraySize(const FieldPath &inPath, uint32_t &outSize) const
892 {
893     (void)inPath;
894     (void)outSize;
895     return -E_NOT_PERMIT;
896 }
897 
898 int JsonObject::GetArrayContentOfStringOrStringArray(const FieldPath &inPath,
899     std::vector<std::vector<std::string>> &outContent) const
900 {
901     (void)inPath;
902     (void)outContent;
903     return -E_NOT_PERMIT;
904 }
905 
906 int JsonObject::InsertField(const FieldPath &inPath, FieldType inType, const FieldValue &inValue)
907 {
908     (void)inPath;
909     (void)inType;
910     (void)inValue;
911     return -E_NOT_PERMIT;
912 }
913 
914 int JsonObject::InsertField(const FieldPath &inPath, const JsonObject &inValue, bool isAppend = false)
915 {
916     (void)inPath;
917     (void)inValue;
918     (void)isAppend;
919     return -E_NOT_PERMIT;
920 }
921 
922 int JsonObject::DeleteField(const FieldPath &inPath)
923 {
924     (void)inPath;
925     return -E_NOT_PERMIT;
926 }
927 
928 int JsonObject::GetArrayValueByFieldPath(const FieldPath &inPath, JsonObject &outArray) const
929 {
930     (void)inPath;
931     (void)outArray;
932     return -E_NOT_PERMIT;
933 }
934 #endif // OMIT_JSON
935 } // namespace DistributedDB
936