1 /*
2  * Copyright (c) 2021-2022 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 FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_JSON_UTIL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_JSON_UTIL_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "base/utils/macros.h"
23 #include "core/components_ng/base/inspector_filter.h"
24 #include "interfaces/inner_api/ace/serializeable_object.h"
25 
26 struct cJSON;
27 
28 namespace OHOS::Ace {
29 
30 using JsonObject = cJSON;
31 
32 class ACE_FORCE_EXPORT JsonValue : public SerializeableObject {
33 public:
34     JsonValue() = default;
35     explicit JsonValue(JsonObject* object);
36     JsonValue(JsonObject* object, bool isRoot);
37     ~JsonValue() override;
38 
39     // check functions
40     bool IsBool() const;
41     bool IsNumber() const;
42     bool IsString() const;
43     bool IsArray() const;
44     bool IsObject() const;
45     bool IsValid() const;
46     bool IsNull() const;
47     bool Contains(const std::string& key) const override;
48 
49     // get functions
50     bool GetBool() const;
51     bool GetBool(const std::string& key, bool defaultValue = false) const override;
52     int32_t GetInt() const;
53     int32_t GetInt(const std::string& key, int32_t defaultVal = 0) const override;
54     uint32_t GetUInt() const;
55     uint32_t GetUInt(const std::string& key, uint32_t defaultVal = 0) const override;
56     int64_t GetInt64() const;
57     int64_t GetInt64(const std::string& key, int64_t defaultVal = 0) const override;
58     double GetDouble() const;
59     double GetDouble(const std::string& key, double defaultVal = 0.0) const override;
60     std::string GetString() const;
61     std::string GetString(const std::string& key, const std::string& defaultVal = "") const override;
62 
63     std::unique_ptr<JsonValue> GetNext() const;
64     std::unique_ptr<JsonValue> GetChild() const;
65     std::string GetKey() const;
66     virtual std::unique_ptr<JsonValue> GetValue(const std::string& key) const;
67     virtual std::unique_ptr<JsonValue> GetObject(const std::string& key) const;
68     int32_t GetArraySize() const;
69     std::unique_ptr<JsonValue> GetArrayItem(int32_t index) const;
70     const JsonObject* GetJsonObject() const;
71 
72     // put functions
73     bool Put(const char* key, const char* value) override;
74     bool Put(const char* key, size_t value) override;
75     bool Put(const char* key, int32_t value) override;
76     bool Put(const char* key, int64_t value) override;
77     bool Put(const char* key, double value) override;
78     bool Put(const char* key, bool value) override;
79     virtual bool Put(const char* key, const std::unique_ptr<JsonValue>& value);
80     bool Put(const std::unique_ptr<JsonValue>& value);
81 
82     bool PutFixedAttr(const char* key, const char* value, const NG::InspectorFilter& filter, NG::FixedAttrBit attr);
83     bool PutFixedAttr(const char* key, size_t value, const NG::InspectorFilter& filter, NG::FixedAttrBit attr);
84     bool PutFixedAttr(const char* key, int32_t value, const NG::InspectorFilter& filter, NG::FixedAttrBit attr);
85     bool PutFixedAttr(const char* key, int64_t value, const NG::InspectorFilter& filter, NG::FixedAttrBit attr);
86     bool PutFixedAttr(const char* key, double value, const NG::InspectorFilter& filter, NG::FixedAttrBit attr);
87     bool PutFixedAttr(const char* key, bool value, const NG::InspectorFilter& filter, NG::FixedAttrBit attr);
88     virtual bool PutFixedAttr(const char* key, const std::unique_ptr<JsonValue>& value,
89         const NG::InspectorFilter& filter, NG::FixedAttrBit attr);
90 
91     bool PutExtAttr(const char* key, const char* value, const NG::InspectorFilter& filter);
92     bool PutExtAttr(const char* key, size_t value, const NG::InspectorFilter& filter);
93     bool PutExtAttr(const char* key, int32_t value, const NG::InspectorFilter& filter);
94     bool PutExtAttr(const char* key, int64_t value, const NG::InspectorFilter& filter);
95     bool PutExtAttr(const char* key, double value, const NG::InspectorFilter& filter);
96     bool PutExtAttr(const char* key, bool value, const NG::InspectorFilter& filter);
97     virtual bool PutExtAttr(const char* key, const std::unique_ptr<JsonValue>& value,
98         const NG::InspectorFilter& filter);
99 
100     JsonObject* ReleaseJsonObject();
101     bool PutRef(const char* key, std::unique_ptr<JsonValue>&& value);
102     bool PutRef(std::unique_ptr<JsonValue>&& value);
103 
104     // replace functions
105     bool Replace(const char* key, const char* value);
106     bool Replace(const char* key, int32_t value);
107     bool Replace(const char* key, const std::unique_ptr<JsonValue>& value);
108     bool Replace(const char* key, bool value);
109     bool Replace(const char* key, double value);
110 
111     // delete functions
112     bool Delete(const char* key);
113 
114     // serialize
115     std::string ToString() override;
116 
117 private:
118     JsonObject* object_ = nullptr;
119     bool isRoot_ = false;
120 };
121 
122 class ACE_FORCE_EXPORT JsonUtil final {
123 public:
124     JsonUtil() = delete;
125     ~JsonUtil() = delete;
126     static std::unique_ptr<JsonValue> ParseJsonData(const char* data, const char** parseEnd = nullptr);
127     static std::unique_ptr<JsonValue> ParseJsonString(const std::string& content, const char** parseEnd = nullptr);
128     static std::unique_ptr<JsonValue> Create(bool isRoot = true);
129     static std::unique_ptr<JsonValue> CreateArray(bool isRoot = true);
130 };
131 
132 } // namespace OHOS::Ace
133 
134 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_JSON_UTIL_H
135