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