• 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, double value) const;
55     bool Add(const char *key, const char *value) const;
56     bool Add(const char *key, const std::unique_ptr<PtJson> &value) const;
57 
58     // Push back to array
59     bool Push(bool value) const;
60     bool Push(int32_t value) const;
61     bool Push(int64_t value) const;
62     bool Push(double value) const;
63     bool Push(const char *value) const;
64     bool Push(const std::unique_ptr<PtJson> &value) const;
65 
66     // Remove Json child
67     bool Remove(const char *key) const;
68 
69     bool Contains(const char *key) const;
70 
71     std::string GetKey() const;
72 
73     cJSON *GetJson() const;
74 
75     // Type check
76     bool IsBool() const;
77     bool IsNumber() const;
78     bool IsString() const;
79     bool IsObject() const;
80     bool IsArray() const;
81     bool IsNull() const;
82 
83     // Object accessor
84     bool GetBool(bool defaultValue = false) const;
85     int32_t GetInt(int32_t defaultValue = 0) const;
86     int64_t GetInt64(int64_t defaultValue = 0) const;
87     double GetDouble(double defaultValue = 0.0) const;
88     std::string GetString() const;
89 
90     // Array accessor
91     int32_t GetSize() const;
92     std::unique_ptr<PtJson> Get(int32_t index) const;
93 
94     // Child item accessor
95     Result GetBool(const char *key, bool *value) const;
96     Result GetInt(const char *key, int32_t *value) const;
97     Result GetInt64(const char *key, int64_t *value) const;
98     Result GetDouble(const char *key, double *value) const;
99     Result GetString(const char *key, std::string *value) const;
100     Result GetObject(const char *key, std::unique_ptr<PtJson> *value) const;
101     Result GetArray(const char *key, std::unique_ptr<PtJson> *value) const;
102     Result GetAny(const char *key, std::unique_ptr<PtJson> *value) const;
103 
104 private:
105     cJSON *object_ = nullptr;
106 };
107 }  // namespace panda::ecmascript
108 
109 #endif  // ECMASCRIPT_TOOLING_BASE_PT_JSON_H
110