12ee81decSopenharmony_ci/*
22ee81decSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
32ee81decSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42ee81decSopenharmony_ci * you may not use this file except in compliance with the License.
52ee81decSopenharmony_ci * You may obtain a copy of the License at
62ee81decSopenharmony_ci *
72ee81decSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
82ee81decSopenharmony_ci *
92ee81decSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102ee81decSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112ee81decSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122ee81decSopenharmony_ci * See the License for the specific language governing permissions and
132ee81decSopenharmony_ci * limitations under the License.
142ee81decSopenharmony_ci */
152ee81decSopenharmony_ci
162ee81decSopenharmony_ci#include "utils_json.h"
172ee81decSopenharmony_ci
182ee81decSopenharmony_ci#include <stddef.h>
192ee81decSopenharmony_ci
202ee81decSopenharmony_ci#include "cJSON.h"
212ee81decSopenharmony_ci
222ee81decSopenharmony_ci#ifdef __cplusplus
232ee81decSopenharmony_ciextern "C" {
242ee81decSopenharmony_ci#endif
252ee81decSopenharmony_ci
262ee81decSopenharmony_ciDslmJsonHandle DslmCreateJson(const char *data)
272ee81decSopenharmony_ci{
282ee81decSopenharmony_ci    cJSON *root = NULL;
292ee81decSopenharmony_ci
302ee81decSopenharmony_ci    if (data != NULL) {
312ee81decSopenharmony_ci        root = cJSON_Parse(data);
322ee81decSopenharmony_ci    } else {
332ee81decSopenharmony_ci        root = cJSON_CreateObject();
342ee81decSopenharmony_ci    }
352ee81decSopenharmony_ci    return (void *)root;
362ee81decSopenharmony_ci}
372ee81decSopenharmony_ci
382ee81decSopenharmony_civoid DslmDestroyJson(DslmJsonHandle handle)
392ee81decSopenharmony_ci{
402ee81decSopenharmony_ci    if (handle != NULL) {
412ee81decSopenharmony_ci        cJSON_Delete((cJSON *)handle);
422ee81decSopenharmony_ci    }
432ee81decSopenharmony_ci}
442ee81decSopenharmony_ci
452ee81decSopenharmony_ciint32_t DslmGetJsonFieldInt(DslmJsonHandle handle, const char *field)
462ee81decSopenharmony_ci{
472ee81decSopenharmony_ci    int32_t ret = -1;
482ee81decSopenharmony_ci
492ee81decSopenharmony_ci    if (handle == NULL) {
502ee81decSopenharmony_ci        return ret;
512ee81decSopenharmony_ci    }
522ee81decSopenharmony_ci
532ee81decSopenharmony_ci    if (field == NULL) {
542ee81decSopenharmony_ci        return ((cJSON *)handle)->valueint;
552ee81decSopenharmony_ci    }
562ee81decSopenharmony_ci
572ee81decSopenharmony_ci    cJSON *objValue = NULL;
582ee81decSopenharmony_ci
592ee81decSopenharmony_ci    do {
602ee81decSopenharmony_ci        objValue = (cJSON *)DslmGetJsonFieldJson(handle, field);
612ee81decSopenharmony_ci        if (objValue == NULL) {
622ee81decSopenharmony_ci            break;
632ee81decSopenharmony_ci        }
642ee81decSopenharmony_ci        if (!cJSON_IsNumber(objValue)) {
652ee81decSopenharmony_ci            break;
662ee81decSopenharmony_ci        }
672ee81decSopenharmony_ci        ret = objValue->valueint;
682ee81decSopenharmony_ci    } while (0);
692ee81decSopenharmony_ci
702ee81decSopenharmony_ci    return ret;
712ee81decSopenharmony_ci}
722ee81decSopenharmony_ci
732ee81decSopenharmony_ciuint32_t DslmGetJsonFieldIntArray(DslmJsonHandle handle, const char *field, int32_t *array, int32_t arrayLen)
742ee81decSopenharmony_ci{
752ee81decSopenharmony_ci    if (handle == NULL || field == NULL || array == NULL) {
762ee81decSopenharmony_ci        return 0;
772ee81decSopenharmony_ci    }
782ee81decSopenharmony_ci
792ee81decSopenharmony_ci    cJSON *objValue = cJSON_GetObjectItem(handle, field);
802ee81decSopenharmony_ci    if (objValue == NULL) {
812ee81decSopenharmony_ci        return 0;
822ee81decSopenharmony_ci    }
832ee81decSopenharmony_ci    if (!cJSON_IsArray(objValue)) {
842ee81decSopenharmony_ci        return 0;
852ee81decSopenharmony_ci    }
862ee81decSopenharmony_ci
872ee81decSopenharmony_ci    int size = cJSON_GetArraySize(objValue);
882ee81decSopenharmony_ci    if (size > arrayLen) {
892ee81decSopenharmony_ci        size = arrayLen;
902ee81decSopenharmony_ci    }
912ee81decSopenharmony_ci    uint32_t index = 0;
922ee81decSopenharmony_ci    for (int32_t i = 0; i < size; i++) {
932ee81decSopenharmony_ci        cJSON *item = cJSON_GetArrayItem(objValue, i);
942ee81decSopenharmony_ci        if (!cJSON_IsNumber(item)) {
952ee81decSopenharmony_ci            continue;
962ee81decSopenharmony_ci        }
972ee81decSopenharmony_ci        array[index++] = item->valueint;
982ee81decSopenharmony_ci    }
992ee81decSopenharmony_ci
1002ee81decSopenharmony_ci    return index;
1012ee81decSopenharmony_ci}
1022ee81decSopenharmony_ci
1032ee81decSopenharmony_civoid DslmAddFieldBoolToJson(DslmJsonHandle handle, const char *field, bool value)
1042ee81decSopenharmony_ci{
1052ee81decSopenharmony_ci    if (handle == NULL || field == NULL) {
1062ee81decSopenharmony_ci        return;
1072ee81decSopenharmony_ci    }
1082ee81decSopenharmony_ci    (void)cJSON_AddBoolToObject((cJSON *)handle, field, value);
1092ee81decSopenharmony_ci}
1102ee81decSopenharmony_ci
1112ee81decSopenharmony_ciconst char *DslmGetJsonFieldString(DslmJsonHandle handle, const char *field)
1122ee81decSopenharmony_ci{
1132ee81decSopenharmony_ci    if (handle == NULL) {
1142ee81decSopenharmony_ci        return NULL;
1152ee81decSopenharmony_ci    }
1162ee81decSopenharmony_ci    if (field == NULL) {
1172ee81decSopenharmony_ci        return ((cJSON *)handle)->valuestring;
1182ee81decSopenharmony_ci    }
1192ee81decSopenharmony_ci    cJSON *objValue = NULL;
1202ee81decSopenharmony_ci    const char *payload = NULL;
1212ee81decSopenharmony_ci
1222ee81decSopenharmony_ci    do {
1232ee81decSopenharmony_ci        objValue = (cJSON *)DslmGetJsonFieldJson(handle, field);
1242ee81decSopenharmony_ci        if (objValue == NULL) {
1252ee81decSopenharmony_ci            break;
1262ee81decSopenharmony_ci        }
1272ee81decSopenharmony_ci        payload = cJSON_GetStringValue(objValue);
1282ee81decSopenharmony_ci        if (payload == NULL) {
1292ee81decSopenharmony_ci            break;
1302ee81decSopenharmony_ci        }
1312ee81decSopenharmony_ci    } while (0);
1322ee81decSopenharmony_ci    return payload;
1332ee81decSopenharmony_ci}
1342ee81decSopenharmony_ci
1352ee81decSopenharmony_ciDslmJsonHandle DslmGetJsonFieldJson(DslmJsonHandle handle, const char *field)
1362ee81decSopenharmony_ci{
1372ee81decSopenharmony_ci    return cJSON_GetObjectItem((cJSON *)handle, field);
1382ee81decSopenharmony_ci}
1392ee81decSopenharmony_ci
1402ee81decSopenharmony_ciDslmJsonHandle DslmGetJsonFieldJsonArray(DslmJsonHandle handle, uint32_t num)
1412ee81decSopenharmony_ci{
1422ee81decSopenharmony_ci    return cJSON_GetArrayItem((cJSON *)handle, num);
1432ee81decSopenharmony_ci}
1442ee81decSopenharmony_ci
1452ee81decSopenharmony_ciint32_t DslmGetJsonFieldJsonArraySize(DslmJsonHandle handle)
1462ee81decSopenharmony_ci{
1472ee81decSopenharmony_ci    return cJSON_GetArraySize((cJSON *)handle);
1482ee81decSopenharmony_ci}
1492ee81decSopenharmony_ci
1502ee81decSopenharmony_civoid DslmAddFieldIntToJson(DslmJsonHandle handle, const char *field, int32_t value)
1512ee81decSopenharmony_ci{
1522ee81decSopenharmony_ci    if (handle == NULL || field == NULL) {
1532ee81decSopenharmony_ci        return;
1542ee81decSopenharmony_ci    }
1552ee81decSopenharmony_ci    (void)cJSON_AddNumberToObject((cJSON *)handle, field, value);
1562ee81decSopenharmony_ci}
1572ee81decSopenharmony_ci
1582ee81decSopenharmony_civoid DslmAddFieldIntArrayToJson(DslmJsonHandle handle, const char *field, const int32_t *array, int32_t arrayLen)
1592ee81decSopenharmony_ci{
1602ee81decSopenharmony_ci    if (handle == NULL || field == NULL || array == NULL) {
1612ee81decSopenharmony_ci        return;
1622ee81decSopenharmony_ci    }
1632ee81decSopenharmony_ci    cJSON *arrayObj = cJSON_CreateIntArray(array, arrayLen);
1642ee81decSopenharmony_ci    if (arrayObj == NULL) {
1652ee81decSopenharmony_ci        return;
1662ee81decSopenharmony_ci    }
1672ee81decSopenharmony_ci    (void)cJSON_AddItemToObject((cJSON *)handle, field, arrayObj);
1682ee81decSopenharmony_ci}
1692ee81decSopenharmony_ci
1702ee81decSopenharmony_civoid DslmAddFieldStringToJson(DslmJsonHandle handle, const char *field, const char *value)
1712ee81decSopenharmony_ci{
1722ee81decSopenharmony_ci    if (handle == NULL || field == NULL || value == NULL) {
1732ee81decSopenharmony_ci        return;
1742ee81decSopenharmony_ci    }
1752ee81decSopenharmony_ci    (void)cJSON_AddStringToObject((cJSON *)handle, field, value);
1762ee81decSopenharmony_ci}
1772ee81decSopenharmony_ci
1782ee81decSopenharmony_civoid DslmAddFieldJsonToJson(DslmJsonHandle handle, const char *field, DslmJsonHandle json)
1792ee81decSopenharmony_ci{
1802ee81decSopenharmony_ci    if (handle == NULL || field == NULL || json == NULL) {
1812ee81decSopenharmony_ci        return;
1822ee81decSopenharmony_ci    }
1832ee81decSopenharmony_ci    (void)cJSON_AddItemToObject((cJSON *)handle, field, json);
1842ee81decSopenharmony_ci}
1852ee81decSopenharmony_ci
1862ee81decSopenharmony_cichar *DslmConvertJsonToString(DslmJsonHandle handle)
1872ee81decSopenharmony_ci{
1882ee81decSopenharmony_ci    if (handle != NULL) {
1892ee81decSopenharmony_ci        char *ret = cJSON_PrintUnformatted((cJSON *)handle);
1902ee81decSopenharmony_ci        return ret;
1912ee81decSopenharmony_ci    }
1922ee81decSopenharmony_ci    return NULL;
1932ee81decSopenharmony_ci}
1942ee81decSopenharmony_ci
1952ee81decSopenharmony_cibool DslmCompareJsonData(DslmJsonHandle handleA, DslmJsonHandle handleB, bool caseSensitive)
1962ee81decSopenharmony_ci{
1972ee81decSopenharmony_ci    if (handleA == NULL || handleB == NULL) {
1982ee81decSopenharmony_ci        return false;
1992ee81decSopenharmony_ci    }
2002ee81decSopenharmony_ci    return cJSON_Compare(handleA, handleB, caseSensitive);
2012ee81decSopenharmony_ci}
2022ee81decSopenharmony_ci
2032ee81decSopenharmony_ci#ifdef __cplusplus
2042ee81decSopenharmony_ci}
2052ee81decSopenharmony_ci#endif