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 26 extern "C" { 27 #endif 28 29 typedef cJSON CJson; 30 31 /* Need to call FreeJson to free the returned pointer when it's no longer in use. */ 32 CJson *CreateJsonFromString(const char *jsonStr); 33 /* Need to call FreeJson to free the returned pointer when it's no longer in use. */ 34 CJson *CreateJson(void); 35 /* Need to call FreeJson to free the returned pointer when it's no longer in use. */ 36 CJson *CreateJsonArray(void); 37 /* Need to call FreeJson to free the returned pointer when it's no longer in use. */ 38 CJson *DuplicateJson(const CJson *jsonObj); 39 void FreeJson(CJson *jsonObj); 40 41 void DeleteItemFromJson(CJson *jsonObj, const char *key); 42 void DeleteAllItemExceptOne(CJson *jsonObj, const char *key); 43 void DeleteAllItem(CJson *jsonObj); 44 CJson *DetachItemFromJson(CJson *jsonObj, const char *key); 45 46 /* Need to call FreeJsonString to free the returned pointer when it's no longer in use. */ 47 char *PackJsonToString(const CJson *jsonObj); 48 void FreeJsonString(char *jsonStr); 49 50 int 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 */ 56 const 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 */ 63 CJson *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 */ 70 CJson *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 */ 77 const 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 */ 83 int32_t GetByteFromJson(const CJson *jsonObj, const char *key, uint8_t *byte, uint32_t len); 84 int32_t GetByteLenFromJson(const CJson *jsonObj, const char *key, uint32_t *byteLen); 85 int32_t GetIntFromJson(const CJson *jsonObj, const char *key, int32_t *value); 86 int32_t GetInt64FromJson(const CJson *jsonObj, const char *key, int64_t *value); 87 int32_t GetBoolFromJson(const CJson *jsonObj, const char *key, bool *value); 88 char *GetStringValue(const CJson *item); 89 90 int32_t AddObjToJson(CJson *jsonObj, const char *key, const CJson *childObj); 91 int32_t AddObjToArray(CJson *jsonArr, CJson *item); 92 int32_t AddStringToJson(CJson *jsonObj, const char *key, const char *value); 93 int32_t AddStringToArray(CJson *jsonArr, const char *string); 94 /* The function will convert the byte to hex string, and then add it to object. */ 95 int32_t AddByteToJson(CJson *jsonObj, const char *key, const uint8_t *byte, uint32_t len); 96 int32_t AddBoolToJson(CJson *jsonObj, const char *key, bool value); 97 int32_t AddIntToJson(CJson *jsonObj, const char *key, int value); 98 int32_t AddInt64StringToJson(CJson *jsonObj, const char *key, int64_t value); 99 int32_t AddStringArrayToJson(CJson *jsonObj, const char *key, const char * const *stringArray, uint32_t arrayLen); 100 void ClearSensitiveStringInJson(CJson *jsonObj, const char *key); 101 void ClearAndFreeJsonString(char *jsonStr); 102 int32_t GetUnsignedIntFromJson(const CJson *jsonObj, const char *key, uint32_t *value); 103 104 #ifdef __cplusplus 105 } 106 #endif 107 #endif 108