166f3657fSopenharmony_ci/* 266f3657fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 366f3657fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 466f3657fSopenharmony_ci * you may not use this file except in compliance with the License. 566f3657fSopenharmony_ci * You may obtain a copy of the License at 666f3657fSopenharmony_ci * 766f3657fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 866f3657fSopenharmony_ci * 966f3657fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1066f3657fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1166f3657fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1266f3657fSopenharmony_ci * See the License for the specific language governing permissions and 1366f3657fSopenharmony_ci * limitations under the License. 1466f3657fSopenharmony_ci */ 1566f3657fSopenharmony_ci 1666f3657fSopenharmony_ci#include "dscreen_json_util.h" 1766f3657fSopenharmony_ci 1866f3657fSopenharmony_ci#include "dscreen_constants.h" 1966f3657fSopenharmony_ci#include "dscreen_log.h" 2066f3657fSopenharmony_ci 2166f3657fSopenharmony_cinamespace OHOS { 2266f3657fSopenharmony_cinamespace DistributedHardware { 2366f3657fSopenharmony_cibool IsString(const nlohmann::json &jsonObj, const std::string &key) 2466f3657fSopenharmony_ci{ 2566f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_string() && jsonObj[key].size() <= MAX_MESSAGES_LEN; 2666f3657fSopenharmony_ci if (!res) { 2766f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 2866f3657fSopenharmony_ci } 2966f3657fSopenharmony_ci return res; 3066f3657fSopenharmony_ci} 3166f3657fSopenharmony_ci 3266f3657fSopenharmony_cibool IsUInt8(const nlohmann::json &jsonObj, const std::string &key) 3366f3657fSopenharmony_ci{ 3466f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT8_MAX; 3566f3657fSopenharmony_ci if (!res) { 3666f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 3766f3657fSopenharmony_ci } 3866f3657fSopenharmony_ci return res; 3966f3657fSopenharmony_ci} 4066f3657fSopenharmony_ci 4166f3657fSopenharmony_cibool IsInt32(const nlohmann::json &jsonObj, const std::string &key) 4266f3657fSopenharmony_ci{ 4366f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT32_MIN && 4466f3657fSopenharmony_ci jsonObj[key] <= INT32_MAX; 4566f3657fSopenharmony_ci if (!res) { 4666f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 4766f3657fSopenharmony_ci } 4866f3657fSopenharmony_ci return res; 4966f3657fSopenharmony_ci} 5066f3657fSopenharmony_ci 5166f3657fSopenharmony_cibool IsUInt32(const nlohmann::json &jsonObj, const std::string &key) 5266f3657fSopenharmony_ci{ 5366f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT32_MAX; 5466f3657fSopenharmony_ci if (!res) { 5566f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 5666f3657fSopenharmony_ci } 5766f3657fSopenharmony_ci return res; 5866f3657fSopenharmony_ci} 5966f3657fSopenharmony_ci 6066f3657fSopenharmony_cibool IsInt64(const nlohmann::json &jsonObj, const std::string &key) 6166f3657fSopenharmony_ci{ 6266f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT64_MIN && 6366f3657fSopenharmony_ci jsonObj[key] <= INT64_MAX; 6466f3657fSopenharmony_ci if (!res) { 6566f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 6666f3657fSopenharmony_ci } 6766f3657fSopenharmony_ci return res; 6866f3657fSopenharmony_ci} 6966f3657fSopenharmony_ci 7066f3657fSopenharmony_cibool IsUInt64(const nlohmann::json &jsonObj, const std::string &key) 7166f3657fSopenharmony_ci{ 7266f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT64_MAX; 7366f3657fSopenharmony_ci if (!res) { 7466f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 7566f3657fSopenharmony_ci } 7666f3657fSopenharmony_ci return res; 7766f3657fSopenharmony_ci} 7866f3657fSopenharmony_ci 7966f3657fSopenharmony_cibool IsFloat(const nlohmann::json &jsonObj, const std::string &key) 8066f3657fSopenharmony_ci{ 8166f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_number_float(); 8266f3657fSopenharmony_ci if (!res) { 8366f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 8466f3657fSopenharmony_ci } 8566f3657fSopenharmony_ci return res; 8666f3657fSopenharmony_ci} 8766f3657fSopenharmony_ci 8866f3657fSopenharmony_cibool IsArray(const nlohmann::json &jsonObj, const std::string &key) 8966f3657fSopenharmony_ci{ 9066f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_array(); 9166f3657fSopenharmony_ci if (!res) { 9266f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 9366f3657fSopenharmony_ci } 9466f3657fSopenharmony_ci return res; 9566f3657fSopenharmony_ci} 9666f3657fSopenharmony_ci 9766f3657fSopenharmony_cibool IsBool(const nlohmann::json &jsonObj, const std::string &key) 9866f3657fSopenharmony_ci{ 9966f3657fSopenharmony_ci bool res = jsonObj.contains(key) && jsonObj[key].is_boolean(); 10066f3657fSopenharmony_ci if (!res) { 10166f3657fSopenharmony_ci DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str()); 10266f3657fSopenharmony_ci } 10366f3657fSopenharmony_ci return res; 10466f3657fSopenharmony_ci} 10566f3657fSopenharmony_ci} // namespace DistributedHardware 10666f3657fSopenharmony_ci} // namespace OHOS