• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_JSON_PT_JSON_H
17 #define DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_JSON_PT_JSON_H
18 
19 #include <memory>
20 #include <string>
21 
22 #include "cJSON.h"
23 
24 namespace OHOS {
25 namespace AppPackingTool {
26 enum class Result : uint8_t {
27     SUCCESS,
28     NOT_EXIST,
29     TYPE_ERROR,
30 };
31 
32 class PtJson {
33 public:
34     PtJson() = default;
PtJson(cJSON * object)35     explicit PtJson(cJSON *object) : object_(object) {}
36     ~PtJson() = default;
37 
38     // Create empty json object
39     static std::unique_ptr<PtJson> CreateObject();
40     static std::unique_ptr<PtJson> CreateArray();
41 
42     // Release cJSON object memory
43     void ReleaseRoot();
44 
45     // String parse to json
46     static std::unique_ptr<PtJson> Parse(const std::string &data);
47 
48     // To string
49     std::string Stringify() const;
50 
51     // Add Json child
52     bool Add(const char *key, bool value) const;
53     bool Add(const char *key, int32_t value) const;
54     bool Add(const char *key, int64_t value) const;
55     bool Add(const char *key, uint32_t value) const;
56     bool Add(const char *key, double value) const;
57     bool Add(const char *key, const char *value) const;
58     bool Add(const char *key, const std::unique_ptr<PtJson> &value) const;
59 
60     // Push back to array
61     bool Push(bool value) const;
62     bool Push(int32_t value) const;
63     bool Push(int64_t value) const;
64     bool Push(uint32_t value) const;
65     bool Push(double value) const;
66     bool Push(const char *value) const;
67     bool Push(cJSON *node) const;
68     bool Push(const std::unique_ptr<PtJson> &value) const;
69 
70     // Remove Json child
71     bool Remove(const char *key) const;
72 
73     bool Contains(const char *key) const;
74 
75     std::string GetKey() const;
76 
77     cJSON *GetJson() const;
78 
79     // Type check
80     bool IsBool() const;
81     bool IsNumber() const;
82     bool IsString() const;
83     bool IsObject() const;
84     bool IsArray() const;
85     bool IsNull() const;
86 
87     // Object accessor
88     bool GetBool(bool defaultValue = false) const;
89     int32_t GetInt(int32_t defaultValue = 0) const;
90     int64_t GetInt64(int64_t defaultValue = 0) const;
91     uint32_t GetUInt(uint32_t defaultValue = 0) const;
92     uint64_t GetUInt64(uint64_t defaultValue = 0) const;
93     double GetDouble(double defaultValue = 0.0) const;
94     std::string GetString() const;
95 
96     // Array accessor
97     int32_t GetSize() const;
98     std::unique_ptr<PtJson> Get(int32_t index) const;
99 
100     // Child item accessor
101     Result GetBool(const char *key, bool *value) const;
102     Result GetInt(const char *key, int32_t *value) const;
103     Result GetInt64(const char *key, int64_t *value) const;
104     Result GetUInt(const char *key, uint32_t *value) const;
105     Result GetUInt64(const char *key, uint64_t *value) const;
106     Result GetDouble(const char *key, double *value) const;
107     Result GetString(const char *key, std::string *value) const;
108     Result GetObject(const char *key, std::unique_ptr<PtJson> *value) const;
109     Result GetArray(const char *key, std::unique_ptr<PtJson> *value) const;
110     Result GetAny(const char *key, std::unique_ptr<PtJson> *value) const;
111 
112     Result SetBool(const char *key, const bool& value);
113     Result SetInt(const char *key, const int32_t& value);
114     Result SetInt64(const char *key, const int64_t& value);
115     Result SetUInt(const char *key, const uint32_t& value);
116     Result SetUInt64(const char *key, const uint64_t& value);
117     Result SetDouble(const char *key, const double& value);
118     Result SetString(const char *key, const std::string& value);
119 
120 private:
121     cJSON *object_ = nullptr;
122 };
123 }  // namespace AppPackingTool
124 }  // namespace OHOS
125 #endif  // DEVELOPTOOLS_PACKING_TOOL_APT_FRAMEWORKS_INCLUDE_JSON_PT_JSON_H
126