/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_DISTRIBUTED_JSON_UTIL_H #define OHOS_DISTRIBUTED_JSON_UTIL_H #include #include "appexecfwk_errors.h" #include "bundle_constants.h" #include "hilog_wrapper.h" #include "json_serializer.h" namespace OHOS { namespace DistributedSchedule { enum class JsonType { NULLABLE, BOOLEAN, NUMBER, OBJECT, ARRAY, STRING, }; enum class ArrayType { NUMBER, OBJECT, STRING, NOT_ARRAY, }; template void CheckArrayType( const nlohmann::json &jsonObject, const std::string &key, dataType &data, ArrayType arrayType, int32_t &parseResult) { auto arrays = jsonObject.at(key); if (arrays.empty()) { return; } if (arrays.size() > AppExecFwk::Constants::MAX_JSON_ARRAY_LENGTH) { parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_SIZE_CHECK_ERROR; return; } switch (arrayType) { case ArrayType::STRING: for (const auto &array : arrays) { if (!array.is_string()) { parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; } } if (parseResult == ERR_OK) { data = jsonObject.at(key).get(); } break; case ArrayType::OBJECT: for (const auto &array : arrays) { if (!array.is_object()) { parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; break; } } if (parseResult == ERR_OK) { data = jsonObject.at(key).get(); } break; case ArrayType::NUMBER: for (const auto &array : arrays) { if (!array.is_number()) { parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; } } if (parseResult == ERR_OK) { data = jsonObject.at(key).get(); } break; case ArrayType::NOT_ARRAY: HILOG_DEBUG("array %{public}s is not string type", key.c_str()); break; default: HILOG_DEBUG("array %{public}s type error", key.c_str()); break; } } template void GetValueIfFindKey(const nlohmann::json &jsonObject, const nlohmann::detail::iter_impl &end, const std::string &key, dataType &data, JsonType jsonType, bool isNecessary, int32_t &parseResult, ArrayType arrayType) { if (parseResult) { return; } if (jsonObject.find(key) != end) { switch (jsonType) { case JsonType::BOOLEAN: if (!jsonObject.at(key).is_boolean()) { HILOG_DEBUG("type is error %{public}s is not boolean", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; break; } data = jsonObject.at(key).get(); break; case JsonType::NUMBER: if (!jsonObject.at(key).is_number()) { HILOG_DEBUG("type is error %{public}s is not number", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; break; } data = jsonObject.at(key).get(); break; case JsonType::OBJECT: if (!jsonObject.at(key).is_object()) { HILOG_DEBUG("type is error %{public}s is not object", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; break; } data = jsonObject.at(key).get(); break; case JsonType::ARRAY: if (!jsonObject.at(key).is_array()) { HILOG_DEBUG("type is error %{public}s is not array", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; break; } CheckArrayType(jsonObject, key, data, arrayType, parseResult); break; case JsonType::STRING: if (!jsonObject.at(key).is_string()) { HILOG_DEBUG("type is error %{public}s is not string", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; break; } data = jsonObject.at(key).get(); if (jsonObject.at(key).get().length() > AppExecFwk::Constants::MAX_JSON_ELEMENT_LENGTH) { parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_SIZE_CHECK_ERROR; } break; case JsonType::NULLABLE: HILOG_DEBUG("type is error %{public}s is nullable", key.c_str()); break; default: HILOG_DEBUG("type is error %{public}s is not jsonType", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR; } return; } if (isNecessary) { HILOG_DEBUG("profile prop %{public}s is mission", key.c_str()); parseResult = ERR_APPEXECFWK_PARSE_PROFILE_MISSING_PROP; } } template const std::string GetJsonStrFromInfo(T &t) { nlohmann::json json = t; return json.dump(); } template bool ParseInfoFromJsonStr(const char *data, T &t) { if (data == nullptr) { HILOG_DEBUG("%{public}s failed due to data is nullptr", __func__); return false; } nlohmann::json jsonObject = nlohmann::json::parse(data, nullptr, false); if (jsonObject.is_discarded()) { HILOG_DEBUG("%{public}s failed due to data is discarded", __func__); return false; } t = jsonObject.get(); return true; } } // namespace DistributedSchedule } // namespace OHOS #endif // OHOS_DISTRIBUTED_JSON_UTIL_H