1 /*
2  * Copyright (c) 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 
16 #ifndef SOFTBUS_ADAPTER_JSON_H
17 #define SOFTBUS_ADAPTER_JSON_H
18 
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include "stdint.h"
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 typedef struct {
30     void *context;
31 } JsonObj;
32 
33 JsonObj *JSON_CreateObject(void);
34 
35 void JSON_Delete(JsonObj *obj);
36 
37 void JSON_Free(void *obj);
38 
39 /* Note: must use JSON_Free to release memory */
40 char *JSON_PrintUnformatted(const JsonObj *obj);
41 
42 JsonObj *JSON_Parse(const char *str, uint32_t len);
43 
44 bool JSON_AddBoolToObject(JsonObj *obj, const char *key, bool value);
45 
46 bool JSON_GetBoolFromOject(const JsonObj *obj, const char *key, bool *value);
47 
48 bool JSON_AddInt16ToObject(JsonObj *obj, const char *key, int16_t value);
49 
50 bool JSON_GetInt16FromOject(const JsonObj *obj, const char *key, int16_t *value);
51 
52 bool JSON_AddInt32ToObject(JsonObj *obj, const char *key, int32_t value);
53 
54 bool JSON_GetInt32FromOject(const JsonObj *obj, const char *key, int32_t *value);
55 
56 bool JSON_AddInt64ToObject(JsonObj *obj, const char *key, int64_t value);
57 
58 bool JSON_GetInt64FromOject(const JsonObj *obj, const char *key, int64_t *value);
59 
60 bool JSON_AddStringToObject(JsonObj *obj, const char *key, const char *value);
61 
62 bool JSON_GetStringFromOject(const JsonObj *obj, const char *key, char *value, uint32_t size);
63 
64 bool JSON_AddStringArrayToObject(JsonObj *obj, const char *key, const char **value, int32_t len);
65 
66 /* use input parameter len to limit value's max array num and return as real value's max array num */
67 bool JSON_GetStringArrayFromOject(const JsonObj *obj, const char *key, char **value, int32_t *len);
68 
69 bool JSON_AddBytesToObject(JsonObj *obj, const char *key, uint8_t *value, uint32_t size);
70 
71 bool JSON_GetBytesFromObject(const JsonObj *obj, const char *key, uint8_t *value, uint32_t bufLen, uint32_t *size);
72 
73 #ifdef __cplusplus
74 #if __cplusplus
75 }
76 #endif /* __cplusplus */
77 #endif /* __cplusplus */
78 #endif // SOFTBUS_ADAPTER_JSON_H