169570cc8Sopenharmony_ci/* 269570cc8Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License. 569570cc8Sopenharmony_ci * You may obtain a copy of the License at 669570cc8Sopenharmony_ci * 769570cc8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 869570cc8Sopenharmony_ci * 969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and 1369570cc8Sopenharmony_ci * limitations under the License. 1469570cc8Sopenharmony_ci */ 1569570cc8Sopenharmony_ci 1669570cc8Sopenharmony_ci#ifndef JSON_UTILS_H 1769570cc8Sopenharmony_ci#define JSON_UTILS_H 1869570cc8Sopenharmony_ci 1969570cc8Sopenharmony_ci#include <stdbool.h> 2069570cc8Sopenharmony_ci 2169570cc8Sopenharmony_ci#include "appspawn_utils.h" 2269570cc8Sopenharmony_ci#include "cJSON.h" 2369570cc8Sopenharmony_ci 2469570cc8Sopenharmony_ci#ifdef __cplusplus 2569570cc8Sopenharmony_ciextern "C" { 2669570cc8Sopenharmony_ci#endif // __cplusplus 2769570cc8Sopenharmony_ci 2869570cc8Sopenharmony_ci#define MAX_JSON_FILE_LEN 102400 2969570cc8Sopenharmony_citypedef struct TagParseJsonContext ParseJsonContext; 3069570cc8Sopenharmony_citypedef int (*ParseConfig)(const cJSON *root, ParseJsonContext *context); 3169570cc8Sopenharmony_ciint ParseJsonConfig(const char *path, const char *fileName, ParseConfig parseConfig, ParseJsonContext *context); 3269570cc8Sopenharmony_cicJSON *GetJsonObjFromFile(const char *jsonPath); 3369570cc8Sopenharmony_ci 3469570cc8Sopenharmony_ci__attribute__((always_inline)) inline char *GetStringFromJsonObj(const cJSON *json, const char *key) 3569570cc8Sopenharmony_ci{ 3669570cc8Sopenharmony_ci APPSPAWN_CHECK_ONLY_EXPER(key != NULL && json != NULL, NULL); 3769570cc8Sopenharmony_ci APPSPAWN_CHECK(cJSON_IsObject(json), return NULL, "json is not object %{public}s", key); 3869570cc8Sopenharmony_ci cJSON *obj = cJSON_GetObjectItemCaseSensitive(json, key); 3969570cc8Sopenharmony_ci APPSPAWN_CHECK_ONLY_EXPER(obj != NULL, return NULL); 4069570cc8Sopenharmony_ci APPSPAWN_CHECK(cJSON_IsString(obj), return NULL, "json is not string %{public}s", key); 4169570cc8Sopenharmony_ci return cJSON_GetStringValue(obj); 4269570cc8Sopenharmony_ci} 4369570cc8Sopenharmony_ci 4469570cc8Sopenharmony_ci__attribute__((always_inline)) inline bool GetBoolValueFromJsonObj(const cJSON *json, const char *key, bool def) 4569570cc8Sopenharmony_ci{ 4669570cc8Sopenharmony_ci char *value = GetStringFromJsonObj(json, key); 4769570cc8Sopenharmony_ci APPSPAWN_CHECK_ONLY_EXPER(value != NULL, return def); 4869570cc8Sopenharmony_ci 4969570cc8Sopenharmony_ci if (strcmp(value, "true") == 0 || strcmp(value, "ON") == 0 || strcmp(value, "True") == 0) { 5069570cc8Sopenharmony_ci return true; 5169570cc8Sopenharmony_ci } 5269570cc8Sopenharmony_ci return false; 5369570cc8Sopenharmony_ci} 5469570cc8Sopenharmony_ci 5569570cc8Sopenharmony_ci__attribute__((always_inline)) inline uint32_t GetIntValueFromJsonObj(const cJSON *json, const char *key, uint32_t def) 5669570cc8Sopenharmony_ci{ 5769570cc8Sopenharmony_ci APPSPAWN_CHECK(json != NULL, return def, "Invalid json"); 5869570cc8Sopenharmony_ci APPSPAWN_CHECK(cJSON_IsObject(json), return def, "json is not object."); 5969570cc8Sopenharmony_ci return cJSON_GetNumberValue(cJSON_GetObjectItemCaseSensitive(json, key)); 6069570cc8Sopenharmony_ci} 6169570cc8Sopenharmony_ci 6269570cc8Sopenharmony_ci#ifdef __cplusplus 6369570cc8Sopenharmony_ci} 6469570cc8Sopenharmony_ci#endif // __cplusplus 6569570cc8Sopenharmony_ci#endif // JSON_UTILS_H