1/* 2 * Copyright (C) 2021 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 JSON_UTILS_H 17#define JSON_UTILS_H 18 19#include <stdbool.h> 20#include <stdint.h> 21#include <stdio.h> 22#include <stdlib.h> 23#include "cJSON.h" 24 25#ifdef __cplusplus 26extern "C" { 27#endif 28 29typedef cJSON CJson; 30 31/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ 32CJson *CreateJsonFromString(const char *jsonStr); 33/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ 34CJson *CreateJson(void); 35/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ 36CJson *CreateJsonArray(void); 37/* Need to call FreeJson to free the returned pointer when it's no longer in use. */ 38CJson *DuplicateJson(const CJson *jsonObj); 39void FreeJson(CJson *jsonObj); 40 41void DeleteItemFromJson(CJson *jsonObj, const char *key); 42void DeleteAllItemExceptOne(CJson *jsonObj, const char *key); 43void DeleteAllItem(CJson *jsonObj); 44CJson *DetachItemFromJson(CJson *jsonObj, const char *key); 45 46/* Need to call FreeJsonString to free the returned pointer when it's no longer in use. */ 47char *PackJsonToString(const CJson *jsonObj); 48void FreeJsonString(char *jsonStr); 49 50int GetItemNum(const CJson *jsonObj); 51/* 52 * Can't release the returned pointer, otherwise, an exception may occur. 53 * It refers to the parent object(param--jsonObj)'s memory. 54 * It will be recycled along with jsonObj when jsonObj is released. 55 */ 56const char *GetItemKey(const CJson *item); 57 58/* 59 * Can't release the returned pointer, otherwise, an exception may occur. 60 * It refers to the parent object(param--jsonObj)'s memory. 61 * It will be recycled along with jsonObj when jsonObj is released. 62 */ 63CJson *GetObjFromJson(const CJson *jsonObj, const char *key); 64 65/* 66 * Can't release the returned pointer, otherwise, an exception may occur. 67 * It refers to the parent object(param--jsonObj)'s memory. 68 * It will be recycled along with jsonObj when jsonObj is released. 69 */ 70CJson *GetItemFromArray(const CJson *jsonArr, int index); 71 72/* 73 * Can't release the returned pointer, otherwise, an exception may occur. 74 * It refers to the parent object(param--jsonObj)'s memory. 75 * It will be recycled along with jsonObj when jsonObj is released. 76 */ 77const char *GetStringFromJson(const CJson *jsonObj, const char *key); 78 79/* 80 * The byte in jsonObj must be in the form of hex string. 81 * This function will convert the hex string to byte, and then put it in param--byte in the form of byte. 82 */ 83int32_t GetByteFromJson(const CJson *jsonObj, const char *key, uint8_t *byte, uint32_t len); 84int32_t GetByteLenFromJson(const CJson *jsonObj, const char *key, uint32_t *byteLen); 85int32_t GetIntFromJson(const CJson *jsonObj, const char *key, int32_t *value); 86int32_t GetInt64FromJson(const CJson *jsonObj, const char *key, int64_t *value); 87int32_t GetBoolFromJson(const CJson *jsonObj, const char *key, bool *value); 88char *GetStringValue(const CJson *item); 89 90int32_t AddObjToJson(CJson *jsonObj, const char *key, const CJson *childObj); 91int32_t AddObjToArray(CJson *jsonArr, CJson *item); 92int32_t AddStringToJson(CJson *jsonObj, const char *key, const char *value); 93int32_t AddStringToArray(CJson *jsonArr, const char *string); 94/* The function will convert the byte to hex string, and then add it to object. */ 95int32_t AddByteToJson(CJson *jsonObj, const char *key, const uint8_t *byte, uint32_t len); 96int32_t AddBoolToJson(CJson *jsonObj, const char *key, bool value); 97int32_t AddIntToJson(CJson *jsonObj, const char *key, int value); 98int32_t AddInt64StringToJson(CJson *jsonObj, const char *key, int64_t value); 99int32_t AddStringArrayToJson(CJson *jsonObj, const char *key, const char * const *stringArray, uint32_t arrayLen); 100void ClearSensitiveStringInJson(CJson *jsonObj, const char *key); 101void ClearAndFreeJsonString(char *jsonStr); 102int32_t GetUnsignedIntFromJson(const CJson *jsonObj, const char *key, uint32_t *value); 103 104#ifdef __cplusplus 105} 106#endif 107#endif 108