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 #include "common/macros.h" 24 25 namespace panda::ecmascript::tooling { 26 enum class Result : uint8_t { 27 SUCCESS, 28 NOT_EXIST, 29 TYPE_ERROR, 30 }; 31 32 class TOOLCHAIN_EXPORT 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(const std::unique_ptr<PtJson> &value) const; 68 69 // Remove Json child 70 bool Remove(const char *key) const; 71 72 bool Contains(const char *key) const; 73 74 std::string GetKey() const; 75 76 cJSON *GetJson() const; 77 78 // Type check 79 bool IsBool() const; 80 bool IsNumber() const; 81 bool IsString() const; 82 bool IsObject() const; 83 bool IsArray() const; 84 bool IsNull() const; 85 86 // Object accessor 87 bool GetBool(bool defaultValue = false) const; 88 int32_t GetInt(int32_t defaultValue = 0) const; 89 int64_t GetInt64(int64_t defaultValue = 0) const; 90 uint32_t GetUInt(uint32_t defaultValue = 0) const; 91 uint64_t GetUInt64(uint64_t defaultValue = 0) const; 92 double GetDouble(double defaultValue = 0.0) const; 93 std::string GetString() const; 94 95 // Array accessor 96 int32_t GetSize() const; 97 std::unique_ptr<PtJson> Get(int32_t index) const; 98 99 // Child item accessor 100 Result GetBool(const char *key, bool *value) const; 101 Result GetInt(const char *key, int32_t *value) const; 102 Result GetInt64(const char *key, int64_t *value) const; 103 Result GetUInt(const char *key, uint32_t *value) const; 104 Result GetUInt64(const char *key, uint64_t *value) const; 105 Result GetDouble(const char *key, double *value) const; 106 Result GetString(const char *key, std::string *value) const; 107 Result GetObject(const char *key, std::unique_ptr<PtJson> *value) const; 108 Result GetArray(const char *key, std::unique_ptr<PtJson> *value) const; 109 Result GetAny(const char *key, std::unique_ptr<PtJson> *value) const; 110 111 private: 112 cJSON *object_ = nullptr; 113 }; 114 } // namespace panda::ecmascript 115 116 #endif // ECMASCRIPT_TOOLING_BASE_PT_JSON_H 117