1 /* 2 * Copyright (c) 2021-2023 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 #ifndef SOFTBUS_JSON_UTILS_H 16 #define SOFTBUS_JSON_UTILS_H 17 18 #include <stdint.h> 19 20 #include "cJSON.h" 21 #include "string.h" 22 #include "stdbool.h" 23 24 #ifdef __cplusplus 25 #if __cplusplus 26 extern "C" { 27 #endif 28 #endif 29 30 int32_t GetStringItemByJsonObject(const cJSON *json, const char * const string, char *target, uint32_t targetLen); 31 32 bool GetJsonObjectStringItem(const cJSON *json, const char * const string, char *target, uint32_t targetLen); 33 34 bool GetJsonObjectNumber16Item(const cJSON *json, const char * const string, uint16_t *target); 35 36 bool GetJsonObjectNumberItem(const cJSON *json, const char * const string, int32_t *target); 37 38 bool GetJsonObjectSignedNumberItem(const cJSON *json, const char * const string, int32_t *target); 39 40 bool GetJsonObjectNumber64Item(const cJSON *json, const char * const string, int64_t *target); 41 42 bool GetJsonObjectSignedNumber64Item(const cJSON *json, const char * const string, int64_t *target); 43 44 bool GetJsonObjectDoubleItem(const cJSON *json, const char * const string, double *target); 45 46 bool GetJsonObjectBoolItem(const cJSON *json, const char * const string, bool *target); 47 48 bool AddStringToJsonObject(cJSON *json, const char * const string, const char *value); 49 50 bool AddStringArrayToJsonObject(cJSON *json, const char * const string, const char * const *strings, int32_t count); 51 52 bool AddNumber16ToJsonObject(cJSON *json, const char * const string, uint16_t num); 53 54 bool AddNumberToJsonObject(cJSON *json, const char * const string, int32_t num); 55 56 bool AddNumber64ToJsonObject(cJSON *json, const char * const string, int64_t num); 57 58 bool AddBoolToJsonObject(cJSON *json, const char * const string, bool value); 59 60 bool GetJsonObjectInt32Item(const cJSON *json, const char * const string, int32_t *target); 61 62 char *GetDynamicStringItemByJsonObject(const cJSON * const json, const char * const string, uint32_t limit); 63 64 bool AddIntArrayToJsonObject(cJSON *json, const char *string, const int32_t *array, int32_t arrayLen); 65 66 bool GetJsonObjectIntArrayItem(const cJSON *json, const char *string, int32_t *array, int32_t arrayLen); 67 #ifdef __cplusplus 68 #if __cplusplus 69 } 70 #endif /* __cplusplus */ 71 #endif /* __cplusplus */ 72 #endif /* SOFTBUS_JSON_UTILS_H */ 73