1 /* 2 * Copyright (c) 2021-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 FOUNDATION_ACE_INTERFACE_UI_SESSION_JSON_UTIL_H 17 #define FOUNDATION_ACE_INTERFACE_UI_SESSION_JSON_UTIL_H 18 19 #include <memory> 20 #include <string> 21 22 #include "base/utils/macros.h" 23 24 struct cJSON; 25 26 namespace OHOS::Ace { 27 28 using JsonObject = cJSON; 29 30 class ACE_FORCE_EXPORT InspectorJsonValue { 31 public: 32 InspectorJsonValue() = default; 33 explicit InspectorJsonValue(JsonObject* object); 34 InspectorJsonValue(JsonObject* object, bool isRoot); 35 ~InspectorJsonValue(); 36 // put functions 37 bool Put(const char* key, const char* value); 38 bool Put(const char* key, size_t value); 39 bool Put(const char* key, int32_t value); 40 bool Put(const char* key, int64_t value); 41 bool Put(const char* key, double value); 42 bool Put(const char* key, bool value); 43 bool Put(const char* key, const std::unique_ptr<InspectorJsonValue>& value); 44 bool Put(const std::unique_ptr<InspectorJsonValue>& value); 45 bool Replace(const char* key, const char* value); 46 // serialize 47 std::string ToString(); 48 bool IsObject() const; 49 bool Contains(const std::string& key) const; 50 const JsonObject* GetJsonObject() const; 51 52 private: 53 JsonObject* object_ = nullptr; 54 bool isRoot_ = false; 55 }; 56 57 class ACE_FORCE_EXPORT InspectorJsonUtil final { 58 public: 59 InspectorJsonUtil() = delete; 60 ~InspectorJsonUtil() = delete; 61 static std::shared_ptr<InspectorJsonValue> Create(bool isRoot = true); 62 static std::shared_ptr<InspectorJsonValue> CreateArray(bool isRoot = true); 63 }; 64 65 } // namespace OHOS::Ace 66 67 #endif // FOUNDATION_ACE_INTERFACE_UI_SESSION_JSON_UTIL_H