117fd14ceSopenharmony_ci/*
217fd14ceSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
317fd14ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
417fd14ceSopenharmony_ci * you may not use this file except in compliance with the License.
517fd14ceSopenharmony_ci * You may obtain a copy of the License at
617fd14ceSopenharmony_ci *
717fd14ceSopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
817fd14ceSopenharmony_ci *
917fd14ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1017fd14ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1117fd14ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1217fd14ceSopenharmony_ci * See the License for the specific language governing permissions and
1317fd14ceSopenharmony_ci * limitations under the License.
1417fd14ceSopenharmony_ci */
1517fd14ceSopenharmony_ci
1617fd14ceSopenharmony_ci#ifndef JSON_UTILS_H
1717fd14ceSopenharmony_ci#define JSON_UTILS_H
1817fd14ceSopenharmony_ci
1917fd14ceSopenharmony_ci#include <stdbool.h>
2017fd14ceSopenharmony_ci#include <stdint.h>
2117fd14ceSopenharmony_ci#include <stdio.h>
2217fd14ceSopenharmony_ci#include <stdlib.h>
2317fd14ceSopenharmony_ci#include "cJSON.h"
2417fd14ceSopenharmony_ci
2517fd14ceSopenharmony_ci#ifdef __cplusplus
2617fd14ceSopenharmony_ciextern "C" {
2717fd14ceSopenharmony_ci#endif
2817fd14ceSopenharmony_ci
2917fd14ceSopenharmony_citypedef cJSON CJson;
3017fd14ceSopenharmony_ci
3117fd14ceSopenharmony_ci/* Need to call FreeJson to free the returned pointer when it's no longer in use. */
3217fd14ceSopenharmony_ciCJson *CreateJsonFromString(const char *jsonStr);
3317fd14ceSopenharmony_ci/* Need to call FreeJson to free the returned pointer when it's no longer in use. */
3417fd14ceSopenharmony_ciCJson *CreateJson(void);
3517fd14ceSopenharmony_ci/* Need to call FreeJson to free the returned pointer when it's no longer in use. */
3617fd14ceSopenharmony_ciCJson *CreateJsonArray(void);
3717fd14ceSopenharmony_ci/* Need to call FreeJson to free the returned pointer when it's no longer in use. */
3817fd14ceSopenharmony_ciCJson *DuplicateJson(const CJson *jsonObj);
3917fd14ceSopenharmony_civoid FreeJson(CJson *jsonObj);
4017fd14ceSopenharmony_ci
4117fd14ceSopenharmony_civoid DeleteItemFromJson(CJson *jsonObj, const char *key);
4217fd14ceSopenharmony_civoid DeleteAllItemExceptOne(CJson *jsonObj, const char *key);
4317fd14ceSopenharmony_civoid DeleteAllItem(CJson *jsonObj);
4417fd14ceSopenharmony_ciCJson *DetachItemFromJson(CJson *jsonObj, const char *key);
4517fd14ceSopenharmony_ci
4617fd14ceSopenharmony_ci/* Need to call FreeJsonString to free the returned pointer when it's no longer in use. */
4717fd14ceSopenharmony_cichar *PackJsonToString(const CJson *jsonObj);
4817fd14ceSopenharmony_civoid FreeJsonString(char *jsonStr);
4917fd14ceSopenharmony_ci
5017fd14ceSopenharmony_ciint GetItemNum(const CJson *jsonObj);
5117fd14ceSopenharmony_ci/*
5217fd14ceSopenharmony_ci * Can't release the returned pointer, otherwise, an exception may occur.
5317fd14ceSopenharmony_ci * It refers to the parent object(param--jsonObj)'s memory.
5417fd14ceSopenharmony_ci * It will be recycled along with jsonObj when jsonObj is released.
5517fd14ceSopenharmony_ci */
5617fd14ceSopenharmony_ciconst char *GetItemKey(const CJson *item);
5717fd14ceSopenharmony_ci
5817fd14ceSopenharmony_ci/*
5917fd14ceSopenharmony_ci * Can't release the returned pointer, otherwise, an exception may occur.
6017fd14ceSopenharmony_ci * It refers to the parent object(param--jsonObj)'s memory.
6117fd14ceSopenharmony_ci * It will be recycled along with jsonObj when jsonObj is released.
6217fd14ceSopenharmony_ci */
6317fd14ceSopenharmony_ciCJson *GetObjFromJson(const CJson *jsonObj, const char *key);
6417fd14ceSopenharmony_ci
6517fd14ceSopenharmony_ci/*
6617fd14ceSopenharmony_ci * Can't release the returned pointer, otherwise, an exception may occur.
6717fd14ceSopenharmony_ci * It refers to the parent object(param--jsonObj)'s memory.
6817fd14ceSopenharmony_ci * It will be recycled along with jsonObj when jsonObj is released.
6917fd14ceSopenharmony_ci */
7017fd14ceSopenharmony_ciCJson *GetItemFromArray(const CJson *jsonArr, int index);
7117fd14ceSopenharmony_ci
7217fd14ceSopenharmony_ci/*
7317fd14ceSopenharmony_ci * Can't release the returned pointer, otherwise, an exception may occur.
7417fd14ceSopenharmony_ci * It refers to the parent object(param--jsonObj)'s memory.
7517fd14ceSopenharmony_ci * It will be recycled along with jsonObj when jsonObj is released.
7617fd14ceSopenharmony_ci */
7717fd14ceSopenharmony_ciconst char *GetStringFromJson(const CJson *jsonObj, const char *key);
7817fd14ceSopenharmony_ci
7917fd14ceSopenharmony_ci/*
8017fd14ceSopenharmony_ci * The byte in jsonObj must be in the form of hex string.
8117fd14ceSopenharmony_ci * This function will convert the hex string to byte, and then put it in param--byte in the form of byte.
8217fd14ceSopenharmony_ci */
8317fd14ceSopenharmony_ciint32_t GetByteFromJson(const CJson *jsonObj, const char *key, uint8_t *byte, uint32_t len);
8417fd14ceSopenharmony_ciint32_t GetByteLenFromJson(const CJson *jsonObj, const char *key, uint32_t *byteLen);
8517fd14ceSopenharmony_ciint32_t GetIntFromJson(const CJson *jsonObj, const char *key, int32_t *value);
8617fd14ceSopenharmony_ciint32_t GetInt64FromJson(const CJson *jsonObj, const char *key, int64_t *value);
8717fd14ceSopenharmony_ciint32_t GetBoolFromJson(const CJson *jsonObj, const char *key, bool *value);
8817fd14ceSopenharmony_cichar *GetStringValue(const CJson *item);
8917fd14ceSopenharmony_ci
9017fd14ceSopenharmony_ciint32_t AddObjToJson(CJson *jsonObj, const char *key, const CJson *childObj);
9117fd14ceSopenharmony_ciint32_t AddObjToArray(CJson *jsonArr, CJson *item);
9217fd14ceSopenharmony_ciint32_t AddStringToJson(CJson *jsonObj, const char *key, const char *value);
9317fd14ceSopenharmony_ciint32_t AddStringToArray(CJson *jsonArr, const char *string);
9417fd14ceSopenharmony_ci/* The function will convert the byte to hex string, and then add it to object. */
9517fd14ceSopenharmony_ciint32_t AddByteToJson(CJson *jsonObj, const char *key, const uint8_t *byte, uint32_t len);
9617fd14ceSopenharmony_ciint32_t AddBoolToJson(CJson *jsonObj, const char *key, bool value);
9717fd14ceSopenharmony_ciint32_t AddIntToJson(CJson *jsonObj, const char *key, int value);
9817fd14ceSopenharmony_ciint32_t AddInt64StringToJson(CJson *jsonObj, const char *key, int64_t value);
9917fd14ceSopenharmony_ciint32_t AddStringArrayToJson(CJson *jsonObj, const char *key, const char * const *stringArray, uint32_t arrayLen);
10017fd14ceSopenharmony_civoid ClearSensitiveStringInJson(CJson *jsonObj, const char *key);
10117fd14ceSopenharmony_civoid ClearAndFreeJsonString(char *jsonStr);
10217fd14ceSopenharmony_ciint32_t GetUnsignedIntFromJson(const CJson *jsonObj, const char *key, uint32_t *value);
10317fd14ceSopenharmony_ci
10417fd14ceSopenharmony_ci#ifdef __cplusplus
10517fd14ceSopenharmony_ci}
10617fd14ceSopenharmony_ci#endif
10717fd14ceSopenharmony_ci#endif
108