xref: /ide/tools/previewer/util/JsonReader.cpp (revision 7c804472)
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 "JsonReader.h"
17
18#include <fstream>
19#include <sstream>
20#include <limits>
21#include <cstdint>
22#include "PreviewerEngineLog.h"
23#include "cJSON.h"
24
25namespace Json2 {
26    Value::Value(cJSON* object) : jsonPtr(object), rootNode(true) {}
27
28    Value::Value(cJSON* object, bool isRoot) : jsonPtr(object), rootNode(isRoot) {}
29
30    Value::~Value()
31    {
32        if (!jsonPtr) {
33            return;
34        }
35        if (rootNode) {
36            cJSON_Delete(jsonPtr);
37        }
38        jsonPtr = nullptr;
39    }
40
41    Value Value::operator[](const char* key)
42    {
43        if (!cJSON_HasObjectItem(jsonPtr, key)) {
44            return Value();
45        }
46        return Value(cJSON_GetObjectItemCaseSensitive(jsonPtr, key), false);
47    }
48
49    const Value Value::operator[](const char* key) const
50    {
51        if (!cJSON_HasObjectItem(jsonPtr, key)) {
52            return Value();
53        }
54        return Value(cJSON_GetObjectItemCaseSensitive(jsonPtr, key), false);
55    }
56
57    Value Value::operator[](const std::string& key)
58    {
59        if (!cJSON_HasObjectItem(jsonPtr, key.c_str())) {
60            return Value();
61        }
62        return Value(cJSON_GetObjectItemCaseSensitive(jsonPtr, key.c_str()), false);
63    }
64
65    const Value Value::operator[](const std::string& key) const
66    {
67        if (!cJSON_HasObjectItem(jsonPtr, key.c_str())) {
68            return Value();
69        }
70        return Value(cJSON_GetObjectItemCaseSensitive(jsonPtr, key.c_str()), false);
71    }
72
73    Value::Members Value::GetMemberNames() const
74    {
75        Members names;
76        if (jsonPtr) {
77            cJSON* item = jsonPtr->child;
78            while (item != nullptr) {
79                names.push_back(item->string);
80                item = item->next;
81            }
82        }
83        return names;
84    }
85
86    std::string Value::ToString() const
87    {
88        std::string ret;
89        if (!jsonPtr) {
90            return ret;
91        }
92        char* jsonData = cJSON_PrintUnformatted(jsonPtr);
93        if (jsonData) {
94            ret = jsonData;
95            cJSON_free(jsonData);
96        }
97        return ret;
98    }
99
100    std::string Value::ToStyledString() const
101    {
102        std::string ret;
103        if (!jsonPtr) {
104            return ret;
105        }
106        char* jsonData = cJSON_Print(jsonPtr);
107        if (jsonData) {
108            ret = jsonData;
109            cJSON_free(jsonData);
110        }
111        return ret;
112    }
113
114    const cJSON* Value::GetJsonPtr() const
115    {
116        return jsonPtr;
117    }
118
119    bool Value::IsNull() const
120    {
121        return !jsonPtr || cJSON_IsNull(jsonPtr);
122    }
123
124    bool Value::IsValid() const
125    {
126        return jsonPtr && !cJSON_IsInvalid(jsonPtr);
127    }
128
129    bool Value::IsNumber() const
130    {
131        return cJSON_IsNumber(jsonPtr);
132    }
133
134    bool Value::IsInt() const
135    {
136        if (!IsNumber()) {
137            return false;
138        }
139        double num = cJSON_GetNumberValue(jsonPtr);
140        return (num >= static_cast<double>(std::numeric_limits<int32_t>::min())) &&
141            (num <= static_cast<double>(std::numeric_limits<int32_t>::max()));
142    }
143
144    bool Value::IsUInt() const
145    {
146        if (!IsNumber()) {
147            return false;
148        }
149        double num = cJSON_GetNumberValue(jsonPtr);
150        return (num >= static_cast<double>(std::numeric_limits<uint32_t>::min())) &&
151            (num <= static_cast<double>(std::numeric_limits<uint32_t>::max()));
152    }
153
154    bool Value::IsInt64() const
155    {
156        if (!IsNumber()) {
157            return false;
158        }
159        double num = cJSON_GetNumberValue(jsonPtr);
160        return (num >= static_cast<double>(std::numeric_limits<int64_t>::min())) &&
161            (num <= static_cast<double>(std::numeric_limits<int64_t>::max()));
162    }
163
164    bool Value::IsUInt64() const
165    {
166        if (!IsNumber()) {
167            return false;
168        }
169        double num = cJSON_GetNumberValue(jsonPtr);
170        return (num >= static_cast<double>(std::numeric_limits<uint64_t>::min())) &&
171            (num <= static_cast<double>(std::numeric_limits<uint64_t>::max()));
172    }
173
174    bool Value::IsDouble() const
175    {
176        if (!IsNumber()) {
177            return false;
178        }
179        double num = cJSON_GetNumberValue(jsonPtr);
180        return (num >= std::numeric_limits<double>::lowest()) && (num <= std::numeric_limits<double>::max());
181    }
182
183    bool Value::IsBool() const
184    {
185        return cJSON_IsBool(jsonPtr);
186    }
187
188    bool Value::IsString() const
189    {
190        return cJSON_IsString(jsonPtr);
191    }
192
193    bool Value::IsObject() const
194    {
195        return cJSON_IsObject(jsonPtr);
196    }
197
198    bool Value::IsArray() const
199    {
200        return cJSON_IsArray(jsonPtr);
201    }
202
203    bool Value::IsMember(const char* key) const
204    {
205        return cJSON_HasObjectItem(jsonPtr, key);
206    }
207
208    int32_t Value::GetInt(const char* key, int32_t defaultVal) const
209    {
210        return static_cast<int32_t>(GetDouble(key, defaultVal));
211    }
212
213    uint32_t Value::GetUInt(const char* key, int32_t defaultVal) const
214    {
215        return static_cast<uint32_t>(GetDouble(key, defaultVal));
216    }
217
218    int64_t Value::GetInt64(const char* key, int32_t defaultVal) const
219    {
220        return static_cast<int64_t>(GetDouble(key, defaultVal));
221    }
222
223    float Value::GetFloat(const char* key, float defaultVal) const
224    {
225        return static_cast<float>(GetDouble(key, defaultVal));
226    }
227
228    double Value::GetDouble(const char* key, double defaultVal) const
229    {
230        Value val = GetValue(key);
231        if (!val.IsNull() && val.IsNumber()) {
232            return val.AsDouble();
233        }
234        return defaultVal;
235    }
236
237    bool Value::GetBool(const char* key, bool defaultVal) const
238    {
239        Value val = GetValue(key);
240        if (!val.IsNull() && val.IsBool()) {
241            return val.AsBool();
242        }
243        return defaultVal;
244    }
245
246    std::string Value::GetString(const char* key, const std::string defaultVal) const
247    {
248        Value val = GetValue(key);
249        if (!val.IsNull() && val.IsString()) {
250            return val.AsString();
251        }
252        return defaultVal;
253    }
254
255    Value Value::GetValue(const char* key) const
256    {
257        return Value(cJSON_GetObjectItemCaseSensitive(jsonPtr, key), false);
258    }
259
260    int32_t Value::AsInt() const
261    {
262        return static_cast<int32_t>(AsDouble());
263    }
264
265    uint32_t Value::AsUInt() const
266    {
267        return static_cast<uint32_t>(AsDouble());
268    }
269
270    int64_t Value::AsInt64() const
271    {
272        return static_cast<int64_t>(AsDouble());
273    }
274
275    float Value::AsFloat() const
276    {
277        return static_cast<float>(AsDouble());
278    }
279
280    double Value::AsDouble() const
281    {
282        if (jsonPtr && cJSON_IsNumber(jsonPtr)) {
283            return cJSON_GetNumberValue(jsonPtr);
284        }
285        return 0.0;
286    }
287
288    bool Value::AsBool() const
289    {
290        if (jsonPtr && cJSON_IsBool(jsonPtr)) {
291            return cJSON_IsTrue(jsonPtr);
292        }
293        return false;
294    }
295
296    std::string Value::AsString() const
297    {
298        if (jsonPtr && cJSON_IsString(jsonPtr)) {
299            return std::string(cJSON_GetStringValue(jsonPtr));
300        }
301        return "";
302    }
303
304
305    bool Value::Add(const char* key, const char* value)
306    {
307        if (!key || !cJSON_IsObject(jsonPtr)) {
308            return false;
309        }
310        cJSON* child = cJSON_CreateString(value);
311        if (child == nullptr) {
312            return false;
313        }
314        cJSON_AddItemToObject(jsonPtr, key, child);
315        return true;
316    }
317
318    bool Value::Add(const char* key, bool value)
319    {
320        if (!key || !cJSON_IsObject(jsonPtr)) {
321            return false;
322        }
323        cJSON* child = cJSON_CreateBool(static_cast<int>(value));
324        if (child == nullptr) {
325            return false;
326        }
327        cJSON_AddItemToObject(jsonPtr, key, child);
328        return true;
329    }
330
331    bool Value::Add(const char* key, int32_t value)
332    {
333        return Add(key, static_cast<double>(value));
334    }
335
336    bool Value::Add(const char* key, uint32_t value)
337    {
338        return Add(key, static_cast<double>(value));
339    }
340
341    bool Value::Add(const char* key, int64_t value)
342    {
343        return Add(key, static_cast<double>(value));
344    }
345
346    bool Value::Add(const char* key, double value)
347    {
348        if (!key || !cJSON_IsObject(jsonPtr)) {
349            return false;
350        }
351        cJSON* child = cJSON_CreateNumber(value);
352        if (child == nullptr) {
353            return false;
354        }
355        cJSON_AddItemToObject(jsonPtr, key, child);
356        return true;
357    }
358
359    bool Value::Add(const char* key, const Value& value)
360    {
361        if (!key || value.IsNull() || !value.IsValid()) {
362            return false;
363        }
364        cJSON* jsonObject = cJSON_Duplicate(const_cast<cJSON*>(value.GetJsonPtr()), true);
365        if (jsonObject == nullptr) {
366            return false;
367        }
368        cJSON_AddItemToObject(jsonPtr, key, jsonObject);
369        return true;
370    }
371
372    bool Value::Add(const char* value)
373    {
374        if (!cJSON_IsArray(jsonPtr)) {
375            return false;
376        }
377        cJSON* child = cJSON_CreateString(value);
378        if (child == nullptr) {
379            return false;
380        }
381        cJSON_AddItemToArray(jsonPtr, child);
382        return true;
383    }
384
385    bool Value::Add(bool value)
386    {
387        if (!cJSON_IsArray(jsonPtr)) {
388            return false;
389        }
390        cJSON* child = cJSON_CreateBool(static_cast<int>(value));
391        if (child == nullptr) {
392            return false;
393        }
394        cJSON_AddItemToArray(jsonPtr, child);
395        return true;
396    }
397
398    bool Value::Add(int32_t value)
399    {
400        return Add(static_cast<double>(value));
401    }
402
403    bool Value::Add(uint32_t value)
404    {
405        return Add(static_cast<double>(value));
406    }
407
408    bool Value::Add(int64_t value)
409    {
410        return Add(static_cast<double>(value));
411    }
412
413    bool Value::Add(double value)
414    {
415        if (!cJSON_IsArray(jsonPtr)) {
416            return false;
417        }
418        cJSON* child = cJSON_CreateNumber(value);
419        if (child == nullptr) {
420            return false;
421        }
422        cJSON_AddItemToArray(jsonPtr, child);
423        return true;
424    }
425
426
427    bool Value::Add(const Value& value)
428    {
429        if (value.IsNull() || !value.IsValid()) {
430            return false;
431        }
432        cJSON* jsonObject = cJSON_Duplicate(const_cast<cJSON*>(value.GetJsonPtr()), true);
433        if (jsonObject == nullptr) {
434            return false;
435        }
436        cJSON_AddItemToArray(jsonPtr, jsonObject);
437        return true;
438    }
439
440    bool Value::Replace(const char* key, bool value)
441    {
442        if (!key) {
443            return false;
444        }
445        cJSON* child = cJSON_CreateBool(static_cast<int>(value));
446        if (child == nullptr) {
447            return false;
448        }
449        if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonPtr, key, child)) {
450            cJSON_Delete(child);
451            return false;
452        }
453        return true;
454    }
455
456    bool Value::Replace(const char* key, int32_t value)
457    {
458        return Replace(key, static_cast<double>(value));
459    }
460
461    bool Value::Replace(const char* key, uint32_t value)
462    {
463        return Replace(key, static_cast<double>(value));
464    }
465
466    bool Value::Replace(const char* key, int64_t value)
467    {
468        return Replace(key, static_cast<double>(value));
469    }
470
471    bool Value::Replace(const char* key, double value)
472    {
473        if (!key) {
474            return false;
475        }
476        cJSON* child = cJSON_CreateNumber(value);
477        if (child == nullptr) {
478            return false;
479        }
480        if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonPtr, key, child)) {
481            cJSON_Delete(child);
482            return false;
483        }
484        return true;
485    }
486
487    bool Value::Replace(const char* key, const char* value)
488    {
489        if (!key) {
490            return false;
491        }
492        cJSON* child = cJSON_CreateString(value);
493        if (child == nullptr) {
494            return false;
495        }
496        if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonPtr, key, child)) {
497            cJSON_Delete(child);
498            return false;
499        }
500        return true;
501    }
502
503    bool Value::Replace(const char* key, const Value& value)
504    {
505        if (!key) {
506            return false;
507        }
508        cJSON* jsonObject = cJSON_Duplicate(const_cast<cJSON*>(value.GetJsonPtr()), true);
509        if (jsonObject == nullptr) {
510            return false;
511        }
512
513        if (!cJSON_ReplaceItemInObjectCaseSensitive(jsonPtr, key, jsonObject)) {
514            cJSON_Delete(jsonObject);
515            return false;
516        }
517        return true;
518    }
519
520    bool Value::Replace(int index, bool value)
521    {
522        if (index < 0 || index >= GetArraySize()) {
523            return false;
524        }
525        cJSON* child = cJSON_CreateBool(static_cast<int>(value));
526        if (child == nullptr) {
527            return false;
528        }
529        if (!cJSON_ReplaceItemInArray(jsonPtr, index, child)) {
530            cJSON_Delete(child);
531            return false;
532        }
533        return true;
534    }
535
536    bool Value::Replace(int index, int32_t value)
537    {
538        return Replace(index, static_cast<double>(value));
539    }
540
541    bool Value::Replace(int index, uint32_t value)
542    {
543        return Replace(index, static_cast<double>(value));
544    }
545
546    bool Value::Replace(int index, int64_t value)
547    {
548        return Replace(index, static_cast<double>(value));
549    }
550
551    bool Value::Replace(int index, double value)
552    {
553        if (index < 0 || index >= GetArraySize()) {
554            return false;
555        }
556        cJSON* child = cJSON_CreateNumber(value);
557        if (child == nullptr) {
558            return false;
559        }
560        if (!cJSON_ReplaceItemInArray(jsonPtr, index, child)) {
561            cJSON_Delete(child);
562            return false;
563        }
564        return true;
565    }
566
567    bool Value::Replace(int index, const char* value)
568    {
569        if (index < 0 || index >= GetArraySize()) {
570            return false;
571        }
572        cJSON* child = cJSON_CreateString(value);
573        if (child == nullptr) {
574            return false;
575        }
576        if (!cJSON_ReplaceItemInArray(jsonPtr, index, child)) {
577            cJSON_Delete(child);
578            return false;
579        }
580        return true;
581    }
582
583    bool Value::Replace(int index, const Value& value)
584    {
585        if (index < 0 || index >= GetArraySize()) {
586            return false;
587        }
588        cJSON* jsonObject = cJSON_Duplicate(const_cast<cJSON*>(value.GetJsonPtr()), true);
589        if (jsonObject == nullptr) {
590            return false;
591        }
592
593        if (!cJSON_ReplaceItemInArray(jsonPtr, index, jsonObject)) {
594            cJSON_Delete(jsonObject);
595            return false;
596        }
597        return true;
598    }
599
600    uint32_t Value::GetArraySize() const
601    {
602        return cJSON_GetArraySize(jsonPtr);
603    }
604
605    Value Value::GetArrayItem(int32_t index) const
606    {
607        return Value(cJSON_GetArrayItem(jsonPtr, index), false);
608    }
609
610    void Value::Clear()
611    {
612        cJSON_Delete(jsonPtr);
613        jsonPtr = cJSON_CreateObject();
614    }
615
616    std::string Value::GetKey()
617    {
618        const char* key = jsonPtr->string;
619        if (key) {
620            return std::string(key);
621        }
622        return std::string();
623    }
624}
625
626
627std::string JsonReader::ReadFile(const std::string& path)
628{
629    std::ifstream inFile(path);
630    if (!inFile.is_open()) {
631        ELOG("JsonReader: Open json file failed.");
632        return std::string();
633    }
634    std::string jsonStr((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
635    inFile.close();
636    return jsonStr;
637}
638
639Json2::Value JsonReader::ParseJsonData2(const std::string& jsonStr)
640{
641    return Json2::Value(cJSON_Parse(jsonStr.c_str()));
642}
643
644std::string JsonReader::GetErrorPtr()
645{
646    const char* err = cJSON_GetErrorPtr();
647    if (err) {
648        return std::string(err);
649    }
650    return std::string();
651}
652
653Json2::Value JsonReader::CreateObject()
654{
655    return Json2::Value(cJSON_CreateObject());
656}
657
658Json2::Value JsonReader::CreateArray()
659{
660    return Json2::Value(cJSON_CreateArray());
661}
662
663Json2::Value JsonReader::CreateBool(const bool value)
664{
665    return Json2::Value(cJSON_CreateBool(value));
666}
667
668Json2::Value JsonReader::CreateString(const std::string& value)
669{
670    return Json2::Value(cJSON_CreateString(value.c_str()));
671}
672
673Json2::Value JsonReader::DepthCopy(const Json2::Value& value)
674{
675    return Json2::Value(cJSON_Duplicate(const_cast<cJSON*>(value.GetJsonPtr()), true));
676}
677
678Json2::Value JsonReader::CreateNull()
679{
680    return Json2::Value(cJSON_CreateNull());
681}