• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 BATTERY_MANAGER_CJSON_UTILS_H
17 #define BATTERY_MANAGER_CJSON_UTILS_H
18 
19 #include <string>
20 
21 #include <cJSON.h>
22 
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace BatteryMgrJsonUtils {
IsEmptyJsonParse(const cJSON * jsonValue)26 inline bool IsEmptyJsonParse(const cJSON* jsonValue)
27 {
28     return jsonValue && (cJSON_IsNull(jsonValue) || (cJSON_IsObject(jsonValue) && (jsonValue->child == nullptr)) ||
29             (cJSON_IsArray(jsonValue) && (cJSON_GetArraySize(jsonValue) == 0)));
30 }
31 
IsValidJsonStringAndNoEmpty(const cJSON * jsonValue)32 inline bool IsValidJsonStringAndNoEmpty(const cJSON* jsonValue)
33 {
34     return jsonValue && cJSON_IsString(jsonValue) && jsonValue->valuestring != nullptr &&
35             strlen(jsonValue->valuestring) > 0;
36 }
37 
IsValidJsonObject(const cJSON * jsonValue)38 inline bool IsValidJsonObject(const cJSON* jsonValue)
39 {
40     return jsonValue && cJSON_IsObject(jsonValue);
41 }
42 
IsValidJsonString(const cJSON * jsonValue)43 inline bool IsValidJsonString(const cJSON* jsonValue)
44 {
45     return jsonValue && cJSON_IsString(jsonValue) && jsonValue->valuestring != nullptr;
46 }
47 
IsValidJsonNumber(const cJSON * jsonValue)48 inline bool IsValidJsonNumber(const cJSON* jsonValue)
49 {
50     return jsonValue && cJSON_IsNumber(jsonValue);
51 }
52 
IsValidJsonArray(const cJSON * jsonValue)53 inline bool IsValidJsonArray(const cJSON* jsonValue)
54 {
55     return jsonValue && cJSON_IsArray(jsonValue);
56 }
57 
IsJsonArrayOrJsonObject(const cJSON * jsonValue)58 inline bool IsJsonArrayOrJsonObject(const cJSON* jsonValue)
59 {
60     return jsonValue && (cJSON_IsArray(jsonValue) || cJSON_IsObject(jsonValue));
61 }
62 } // namespace BatteryMgrJsonUtils
63 } // namespace PowerMgr
64 } // namespace OHOS
65 #endif // BATTERY_MANAGER_CJSON_UTILS_H
66