1 /* 2 * Copyright (C) 2024 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 IMAGE_EFFECT_JSON_HELPER_H 17 #define IMAGE_EFFECT_JSON_HELPER_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "image_effect_marco_define.h" 24 25 struct cJSON; 26 27 namespace OHOS { 28 namespace Media { 29 namespace Effect { 30 31 class EffectJson; 32 using EffectJsonPtr = std::shared_ptr<EffectJson>; 33 using Json = cJSON; 34 35 class EffectJson { 36 public: 37 IMAGE_EFFECT_EXPORT explicit EffectJson(Json *json, bool isRoot = true); 38 IMAGE_EFFECT_EXPORT ~EffectJson(); 39 40 IMAGE_EFFECT_EXPORT bool IsBool() const; 41 IMAGE_EFFECT_EXPORT bool IsNumber() const; 42 IMAGE_EFFECT_EXPORT bool IsString() const; 43 IMAGE_EFFECT_EXPORT bool IsArray() const; 44 IMAGE_EFFECT_EXPORT bool IsObject() const; 45 IMAGE_EFFECT_EXPORT bool IsValid() const; 46 IMAGE_EFFECT_EXPORT bool IsNull() const; 47 IMAGE_EFFECT_EXPORT bool HasElement(const std::string &key) const; 48 49 IMAGE_EFFECT_EXPORT EffectJsonPtr GetElement(const std::string &key); 50 IMAGE_EFFECT_EXPORT int32_t GetInt(const std::string &key, int32_t defaultValue = 0); 51 IMAGE_EFFECT_EXPORT uint32_t GetUInt(const std::string &key, uint32_t defaultValue = 0); 52 IMAGE_EFFECT_EXPORT float GetFloat(const std::string &key, float defaultValue = 0.0f); 53 IMAGE_EFFECT_EXPORT double GetDouble(const std::string &key, double defaultValue = 0.0); 54 IMAGE_EFFECT_EXPORT bool GetBool(const std::string &key, bool defaultValue = false); 55 IMAGE_EFFECT_EXPORT std::string GetString(const std::string &key, const std::string &defaultValue = ""); 56 IMAGE_EFFECT_EXPORT std::vector<EffectJsonPtr> GetArray(const std::string &key); 57 58 IMAGE_EFFECT_EXPORT int32_t GetInt(); 59 IMAGE_EFFECT_EXPORT uint32_t GetUInt(); 60 IMAGE_EFFECT_EXPORT float GetFloat(); 61 IMAGE_EFFECT_EXPORT double GetDouble(); 62 IMAGE_EFFECT_EXPORT bool GetBool(); 63 IMAGE_EFFECT_EXPORT std::string GetString(); 64 IMAGE_EFFECT_EXPORT std::vector<EffectJsonPtr> GetArray(); 65 66 IMAGE_EFFECT_EXPORT bool Put(const std::string &key, EffectJsonPtr &json); 67 IMAGE_EFFECT_EXPORT bool Put(const std::string &key, int32_t value); 68 IMAGE_EFFECT_EXPORT bool Put(const std::string &key, uint32_t value); 69 IMAGE_EFFECT_EXPORT bool Put(const std::string &key, float value); 70 IMAGE_EFFECT_EXPORT bool Put(const std::string &key, double value); 71 IMAGE_EFFECT_EXPORT bool Put(const std::string &key, bool value); 72 IMAGE_EFFECT_EXPORT bool Put(const std::string &key, const std::string &value); 73 IMAGE_EFFECT_EXPORT bool Put(const std::string &key, const char *value); 74 75 // add for array 76 IMAGE_EFFECT_EXPORT bool Add(EffectJsonPtr &json) const; 77 IMAGE_EFFECT_EXPORT bool Add(int32_t value) const; 78 IMAGE_EFFECT_EXPORT bool Add(uint32_t value) const; 79 IMAGE_EFFECT_EXPORT bool Add(float value) const; 80 IMAGE_EFFECT_EXPORT bool Add(double value) const; 81 IMAGE_EFFECT_EXPORT bool Add(bool value) const; 82 IMAGE_EFFECT_EXPORT bool Add(const std::string &value) const; 83 IMAGE_EFFECT_EXPORT bool Add(const char *value) const; 84 85 IMAGE_EFFECT_EXPORT bool Replace(const std::string &key, EffectJsonPtr &json); 86 IMAGE_EFFECT_EXPORT bool Replace(const std::string &key, int32_t value); 87 IMAGE_EFFECT_EXPORT bool Replace(const std::string &key, uint32_t value); 88 IMAGE_EFFECT_EXPORT bool Replace(const std::string &key, float value); 89 IMAGE_EFFECT_EXPORT bool Replace(const std::string &key, double value); 90 IMAGE_EFFECT_EXPORT bool Replace(const std::string &key, bool value); 91 IMAGE_EFFECT_EXPORT bool Replace(const std::string &key, const std::string &value); 92 IMAGE_EFFECT_EXPORT bool Replace(const std::string &key, const char *value); 93 94 IMAGE_EFFECT_EXPORT std::string ToString() const; 95 private: 96 bool Put(const std::string &key, Json *json, bool isAllowDelete = true); 97 bool Add(Json *json, bool isAllowDelete = true) const; 98 bool Replace(const std::string &key, Json *json, bool isAllowDelete = true); 99 Json *json_ = nullptr; 100 bool isRoot_ = false; 101 }; 102 103 class JsonHelper final { 104 public: 105 IMAGE_EFFECT_EXPORT static EffectJsonPtr ParseJsonData(const std::string &data); 106 IMAGE_EFFECT_EXPORT static EffectJsonPtr CreateObject(bool isRoot = true); 107 IMAGE_EFFECT_EXPORT static EffectJsonPtr CreateArray(bool isRoot = true); 108 }; 109 } // namespace Effect 110 } // namespace Media 111 } // namespace OHOS 112 113 #endif // IMAGE_EFFECT_JSON_HELPER_H