• 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 #ifndef JSONREADER_H
17 #define JSONREADER_H
18 
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <unordered_map>
23 #include <variant>
24 #include <optional>
25 
26 struct cJSON;
27 
28 namespace Json2 {
29     class Value {
30     public:
31         Value() = default;
32         explicit Value(cJSON* object);
33         Value(cJSON* object, bool isRoot);
34         ~Value();
35         // 重载实现obj["key"]形式调用
36         Value operator[](const char* key);
37         const Value operator[](const char* key) const;
38         Value operator[](const std::string& key);
39         const Value operator[](const std::string& key) const;
40         // 获取所有成员键值
41         using Members = std::vector<std::string>;
42         Value::Members GetMemberNames() const;
43         // convert string functions
44         std::string ToString() const;
45         std::string ToStyledString() const;
46         const cJSON* GetJsonPtr() const;
47         // check functions
48         bool IsNull() const;
49         bool IsValid() const;
50         bool IsNumber() const;
51         bool IsInt() const;
52         bool IsUInt() const;
53         bool IsInt64() const;
54         bool IsUInt64() const;
55         bool IsDouble() const;
56         bool IsBool() const;
57         bool IsString() const;
58         bool IsObject() const;
59         bool IsArray() const;
60         bool IsMember(const char* key) const;
61         // get functions
62         int32_t GetInt(const char* key, int32_t defaultVal = 0) const;
63         uint32_t GetUInt(const char* key, int32_t defaultVal = 0) const;
64         int64_t GetInt64(const char* key, int32_t defaultVal = 0) const;
65         float GetFloat(const char* key, float defaultVal = 0.0) const;
66         double GetDouble(const char* key, double defaultVal = 0.0) const;
67         bool GetBool(const char* key, bool defaultVal = false) const;
68         std::string GetString(const char* key, const std::string defaultVal = "") const;
69         Value GetValue(const char* key) const;
70         int32_t AsInt() const;
71         uint32_t AsUInt() const;
72         int64_t AsInt64() const;
73         float AsFloat() const;
74         double AsDouble() const;
75         bool AsBool() const;
76         std::string AsString() const;
77         // add functions for obj
78         bool Add(const char* key, const char* value);
79         bool Add(const char* key, bool value);
80         bool Add(const char* key, int32_t value);
81         bool Add(const char* key, uint32_t value);
82         bool Add(const char* key, int64_t value);
83         bool Add(const char* key, double value);
84         bool Add(const char* key, const Value& value);
85         // add functions for array
86         bool Add(const char* value);
87         bool Add(bool value);
88         bool Add(int32_t value);
89         bool Add(uint32_t value);
90         bool Add(int64_t value);
91         bool Add(double value);
92         bool Add(const Value& value);
93         // replace functions for obj
94         bool Replace(const char* key, bool value);
95         bool Replace(const char* key, int32_t value);
96         bool Replace(const char* key, uint32_t value);
97         bool Replace(const char* key, int64_t value);
98         bool Replace(const char* key, double value);
99         bool Replace(const char* key, const char* value);
100         bool Replace(const char* key, const Value& value);
101         // replace functions for array
102         bool Replace(int index, bool value);
103         bool Replace(int index, int32_t value);
104         bool Replace(int index, uint32_t value);
105         bool Replace(int index, int64_t value);
106         bool Replace(int index, double value);
107         bool Replace(int index, const char* value);
108         bool Replace(int index, const Value& value);
109         // array functions
110         uint32_t GetArraySize() const;
111         Value GetArrayItem(int32_t index) const;
112         // empty object
113         void Clear();
114         std::string GetKey();
115 
116     private:
117         cJSON* jsonPtr = nullptr;
118         bool rootNode = true;
119     };
120 }
121 
122 class JsonReader {
123 public:
124     static std::string ReadFile(const std::string& path);
125     static Json2::Value ParseJsonData2(const std::string& jsonStr);
126     static std::string GetErrorPtr();
127     static Json2::Value CreateObject();
128     static Json2::Value CreateArray();
129     static Json2::Value CreateBool(const bool value);
130     static Json2::Value CreateString(const std::string& value);
131     static Json2::Value DepthCopy(const Json2::Value& value);
132     static Json2::Value CreateNull();
133 };
134 
135 #endif // JSONREADER_H
136