1 /* 2 * Copyright (c) 2023 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 JSON_UTILS_H 17 #define JSON_UTILS_H 18 19 #include <algorithm> 20 #include <functional> 21 #include <string> 22 23 #include "nlohmann/json.hpp" 24 25 #include "update_define.h" 26 #include "update_log.h" 27 28 namespace OHOS { 29 namespace UpdateEngine { 30 31 enum class JsonParseError { 32 ERR_OK = 0, 33 COMMOM_ERROR, 34 MISSING_PROP, 35 TYPE_ERROR 36 }; 37 38 class JsonUtils { 39 public: 40 template <typename T> GetValueAndSetTo(const nlohmann::json & jsonObject,const std::string & key,T & value)41 static int32_t GetValueAndSetTo(const nlohmann::json &jsonObject, const std::string &key, T &value) 42 { 43 if (jsonObject.find(key) == jsonObject.end()) { 44 ENGINE_LOGI("parse error, missing prop : %{public}s ", key.c_str()); 45 return CAST_INT(JsonParseError::MISSING_PROP); 46 } 47 if (!CheckType(jsonObject, key, value)) { 48 ENGINE_LOGI("type is error : %{public}s ", key.c_str()); 49 return CAST_INT(JsonParseError::TYPE_ERROR); 50 } 51 jsonObject.at(key).get_to(value); 52 return CAST_INT(JsonParseError::ERR_OK); 53 }; 54 ParseAndGetJsonObject(const std::string & jsonStr,nlohmann::json & root)55 static bool ParseAndGetJsonObject(const std::string &jsonStr, nlohmann::json &root) 56 { 57 root = nlohmann::json::parse(jsonStr, nullptr, false); 58 if (root.is_discarded() || !root.is_object()) { 59 return false; 60 } 61 return true; 62 } 63 ParseAndGetJsonArray(const std::string & jsonStr,nlohmann::json & root)64 static bool ParseAndGetJsonArray(const std::string &jsonStr, nlohmann::json &root) 65 { 66 root = nlohmann::json::parse(jsonStr, nullptr, false); 67 if (root.is_discarded() || !root.is_array()) { 68 return false; 69 } 70 return true; 71 } 72 private: CheckType(const nlohmann::json & jsonObject,const std::string & key,std::string & value)73 static bool CheckType(const nlohmann::json &jsonObject, const std::string &key, std::string &value) 74 { 75 return jsonObject.at(key).is_string(); 76 } CheckType(const nlohmann::json & jsonObject,const std::string & key,int32_t & value)77 static bool CheckType(const nlohmann::json &jsonObject, const std::string &key, int32_t &value) 78 { 79 return jsonObject.at(key).is_number(); 80 } CheckType(const nlohmann::json & jsonObject,const std::string & key,uint32_t & value)81 static bool CheckType(const nlohmann::json &jsonObject, const std::string &key, uint32_t &value) 82 { 83 return jsonObject.at(key).is_number(); 84 } CheckType(const nlohmann::json & jsonObject,const std::string & key,uint64_t & value)85 static bool CheckType(const nlohmann::json &jsonObject, const std::string &key, uint64_t &value) 86 { 87 return jsonObject.at(key).is_number(); 88 } CheckType(const nlohmann::json & jsonObject,const std::string & key,bool & value)89 static bool CheckType(const nlohmann::json &jsonObject, const std::string &key, bool &value) 90 { 91 return jsonObject.at(key).is_boolean(); 92 } 93 }; 94 } // namespace UpdateEngine 95 } // namespace OHOS 96 #endif // JSON_UTILS_H