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 #include <vector> 22 23 #include "cJSON.h" 24 #include "common/macros.h" 25 26 namespace panda::ecmascript::tooling { 27 enum class Result : uint8_t { 28 SUCCESS, 29 NOT_EXIST, 30 TYPE_ERROR, 31 }; 32 33 class TOOLCHAIN_EXPORT 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 61 // Push back to array 62 bool Push(bool value) const; 63 bool Push(int32_t value) const; 64 bool Push(int64_t value) const; 65 bool Push(uint32_t value) const; 66 bool Push(double value) const; 67 bool Push(const char *value) 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 std::vector<std::string> GetKeysArray() 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 private: 115 cJSON *object_ = nullptr; 116 }; 117 } // namespace panda::ecmascript 118 119 #endif // ECMASCRIPT_TOOLING_BASE_PT_JSON_H 120