• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "interfaces/inner_api/ace/serializeable_object.h"
24 
25 struct cJSON;
26 
27 namespace OHOS::Ace {
28 
29 using JsonObject = cJSON;
30 
31 class ACE_FORCE_EXPORT JsonValue : public SerializeableObject {
32 public:
33     JsonValue() = default;
34     explicit JsonValue(JsonObject* object);
35     JsonValue(JsonObject* object, bool isRoot);
36     ~JsonValue() override;
37 
38     // check functions
39     bool IsBool() const;
40     bool IsNumber() const;
41     bool IsString() const;
42     bool IsArray() const;
43     bool IsObject() const;
44     bool IsValid() const;
45     bool IsNull() const;
46     bool Contains(const std::string& key) const override;
47 
48     // get functions
49     bool GetBool() const;
50     bool GetBool(const std::string& key, bool defaultValue = false) const override;
51     int32_t GetInt() const;
52     int32_t GetInt(const std::string& key, int32_t defaultVal = 0) const override;
53     uint32_t GetUInt() const;
54     uint32_t GetUInt(const std::string& key, uint32_t defaultVal = 0) const override;
55     int64_t GetInt64() const;
56     int64_t GetInt64(const std::string& key, int64_t defaultVal = 0) const override;
57     double GetDouble() const;
58     double GetDouble(const std::string& key, double defaultVal = 0.0) const override;
59     std::string GetString() const;
60     std::string GetString(const std::string& key, const std::string& defaultVal = "") const override;
61 
62     std::unique_ptr<JsonValue> GetNext() const;
63     std::unique_ptr<JsonValue> GetChild() const;
64     std::string GetKey() const;
65     virtual std::unique_ptr<JsonValue> GetValue(const std::string& key) const;
66     virtual std::unique_ptr<JsonValue> GetObject(const std::string& key) const;
67     int32_t GetArraySize() const;
68     std::unique_ptr<JsonValue> GetArrayItem(int32_t index) const;
69     const JsonObject* GetJsonObject() const;
70 
71     // put functions
72     bool Put(const char* key, const char* value) override;
73     bool Put(const char* key, size_t value) override;
74     bool Put(const char* key, int32_t value) override;
75     bool Put(const char* key, int64_t value) override;
76     bool Put(const char* key, double value) override;
77     bool Put(const char* key, bool value) override;
78     virtual bool Put(const char* key, const std::unique_ptr<JsonValue>& value);
79     bool Put(const std::unique_ptr<JsonValue>& value);
80 
81     // replace functions
82     bool Replace(const char* key, const char* value);
83     bool Replace(const char* key, int32_t value);
84     bool Replace(const char* key, const std::unique_ptr<JsonValue>& value);
85     bool Replace(const char* key, bool value);
86     bool Replace(const char* key, double value);
87 
88     // delete functions
89     bool Delete(const char* key);
90 
91     // serialize
92     std::string ToString() override;
93 
94 private:
95     JsonObject* object_ = nullptr;
96     bool isRoot_ = false;
97 };
98 
99 class ACE_FORCE_EXPORT JsonUtil final {
100 public:
101     JsonUtil() = delete;
102     ~JsonUtil() = delete;
103     static std::unique_ptr<JsonValue> ParseJsonData(const char* data, const char** parseEnd = nullptr);
104     static std::unique_ptr<JsonValue> ParseJsonString(const std::string& content, const char** parseEnd = nullptr);
105     static std::unique_ptr<JsonValue> Create(bool isRoot);
106     static std::unique_ptr<JsonValue> CreateArray(bool isRoot);
107 };
108 
109 } // namespace OHOS::Ace
110 
111 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_JSON_JSON_UTIL_H
112