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