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