1 /*
2  * Copyright (c) 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 #include "dscreen_json_util.h"
17 
18 #include "dscreen_constants.h"
19 #include "dscreen_log.h"
20 
21 namespace OHOS {
22 namespace DistributedHardware {
IsString(const nlohmann::json & jsonObj,const std::string & key)23 bool IsString(const nlohmann::json &jsonObj, const std::string &key)
24 {
25     bool res = jsonObj.contains(key) && jsonObj[key].is_string() && jsonObj[key].size() <= MAX_MESSAGES_LEN;
26     if (!res) {
27         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
28     }
29     return res;
30 }
31 
IsUInt8(const nlohmann::json & jsonObj,const std::string & key)32 bool IsUInt8(const nlohmann::json &jsonObj, const std::string &key)
33 {
34     bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT8_MAX;
35     if (!res) {
36         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
37     }
38     return res;
39 }
40 
IsInt32(const nlohmann::json & jsonObj,const std::string & key)41 bool IsInt32(const nlohmann::json &jsonObj, const std::string &key)
42 {
43     bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT32_MIN &&
44         jsonObj[key] <= INT32_MAX;
45     if (!res) {
46         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
47     }
48     return res;
49 }
50 
IsUInt32(const nlohmann::json & jsonObj,const std::string & key)51 bool IsUInt32(const nlohmann::json &jsonObj, const std::string &key)
52 {
53     bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT32_MAX;
54     if (!res) {
55         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
56     }
57     return res;
58 }
59 
IsInt64(const nlohmann::json & jsonObj,const std::string & key)60 bool IsInt64(const nlohmann::json &jsonObj, const std::string &key)
61 {
62     bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT64_MIN &&
63         jsonObj[key] <= INT64_MAX;
64     if (!res) {
65         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
66     }
67     return res;
68 }
69 
IsUInt64(const nlohmann::json & jsonObj,const std::string & key)70 bool IsUInt64(const nlohmann::json &jsonObj, const std::string &key)
71 {
72     bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT64_MAX;
73     if (!res) {
74         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
75     }
76     return res;
77 }
78 
IsFloat(const nlohmann::json & jsonObj,const std::string & key)79 bool IsFloat(const nlohmann::json &jsonObj, const std::string &key)
80 {
81     bool res = jsonObj.contains(key) && jsonObj[key].is_number_float();
82     if (!res) {
83         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
84     }
85     return res;
86 }
87 
IsArray(const nlohmann::json & jsonObj,const std::string & key)88 bool IsArray(const nlohmann::json &jsonObj, const std::string &key)
89 {
90     bool res = jsonObj.contains(key) && jsonObj[key].is_array();
91     if (!res) {
92         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
93     }
94     return res;
95 }
96 
IsBool(const nlohmann::json & jsonObj,const std::string & key)97 bool IsBool(const nlohmann::json &jsonObj, const std::string &key)
98 {
99     bool res = jsonObj.contains(key) && jsonObj[key].is_boolean();
100     if (!res) {
101         DHLOGE("the key %{public}s in jsonObj is invalid.", key.c_str());
102     }
103     return res;
104 }
105 } // namespace DistributedHardware
106 } // namespace OHOS