FreeRDP
Loading...
Searching...
No Matches
c-json.c
1
20#include <math.h>
21#include <errno.h>
22
23#include <winpr/file.h>
24#include <winpr/json.h>
25#include <winpr/assert.h>
26
27#if !defined(WITH_CJSON)
28#error "This file must only be compiled when cJSON is enabled"
29#endif
30#include <cjson/cJSON.h>
31
32#if defined(WITH_CJSON)
33#if CJSON_VERSION_MAJOR == 1
34#if (CJSON_VERSION_MINOR < 7) || ((CJSON_VERSION_MINOR == 7) && (CJSON_VERSION_PATCH < 13))
35#define USE_CJSON_COMPAT
36#endif
37#endif
38#endif
39
40#if defined(USE_CJSON_COMPAT)
41static double cJSON_GetNumberValue(const cJSON* prop)
42{
43#ifndef NAN
44#ifdef _WIN32
45#define NAN sqrt(-1.0)
46#define COMPAT_NAN_UNDEF
47#else
48#define NAN 0.0 / 0.0
49#define COMPAT_NAN_UNDEF
50#endif
51#endif
52
53 if (!cJSON_IsNumber(prop))
54 return NAN;
55 char* val = cJSON_GetStringValue(prop);
56 if (!val)
57 return NAN;
58
59 errno = 0;
60 char* endptr = NULL;
61 double dval = strtod(val, &endptr);
62 if (val == endptr)
63 return NAN;
64 if (endptr != NULL)
65 return NAN;
66 if (errno != 0)
67 return NAN;
68 return dval;
69
70#ifdef COMPAT_NAN_UNDEF
71#undef NAN
72#endif
73}
74
75static cJSON* cJSON_ParseWithLength(const char* value, size_t buffer_length)
76{
77 // Check for string '\0' termination.
78 const size_t slen = strnlen(value, buffer_length);
79 if (slen >= buffer_length)
80 {
81 if (value[buffer_length] != '\0')
82 return NULL;
83 }
84 return cJSON_Parse(value);
85}
86#endif
87
88int WINPR_JSON_version(char* buffer, size_t len)
89{
90 return _snprintf(buffer, len, "cJSON %s", cJSON_Version());
91}
92
93WINPR_JSON* WINPR_JSON_Parse(const char* value)
94{
95 return cJSON_Parse(value);
96}
97
98WINPR_JSON* WINPR_JSON_ParseWithLength(const char* value, size_t buffer_length)
99{
100 return cJSON_ParseWithLength(value, buffer_length);
101}
102
103void WINPR_JSON_Delete(WINPR_JSON* item)
104{
105 cJSON_Delete((cJSON*)item);
106}
107
108WINPR_JSON* WINPR_JSON_GetArrayItem(const WINPR_JSON* array, size_t index)
109{
110 WINPR_ASSERT(index <= INT_MAX);
111 return cJSON_GetArrayItem((const cJSON*)array, (INT)index);
112}
113
114size_t WINPR_JSON_GetArraySize(const WINPR_JSON* array)
115{
116 const int rc = cJSON_GetArraySize((const cJSON*)array);
117 if (rc <= 0)
118 return 0;
119 return (size_t)rc;
120}
121
122WINPR_JSON* WINPR_JSON_GetObjectItem(const WINPR_JSON* object, const char* string)
123{
124 return cJSON_GetObjectItem((const cJSON*)object, string);
125}
126
127WINPR_JSON* WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON* object, const char* string)
128{
129 return cJSON_GetObjectItemCaseSensitive((const cJSON*)object, string);
130}
131
132BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON* object, const char* string)
133{
134 return cJSON_HasObjectItem((const cJSON*)object, string);
135}
136
137const char* WINPR_JSON_GetErrorPtr(void)
138{
139 return cJSON_GetErrorPtr();
140}
141
142const char* WINPR_JSON_GetStringValue(WINPR_JSON* item)
143{
144 return cJSON_GetStringValue((cJSON*)item);
145}
146
147double WINPR_JSON_GetNumberValue(const WINPR_JSON* item)
148{
149 return cJSON_GetNumberValue((const cJSON*)item);
150}
151
152BOOL WINPR_JSON_IsInvalid(const WINPR_JSON* item)
153{
154 return cJSON_IsInvalid((const cJSON*)item);
155}
156
157BOOL WINPR_JSON_IsFalse(const WINPR_JSON* item)
158{
159 return cJSON_IsFalse((const cJSON*)item);
160}
161
162BOOL WINPR_JSON_IsTrue(const WINPR_JSON* item)
163{
164 return cJSON_IsTrue((const cJSON*)item);
165}
166
167BOOL WINPR_JSON_IsBool(const WINPR_JSON* item)
168{
169 return cJSON_IsBool((const cJSON*)item);
170}
171
172BOOL WINPR_JSON_IsNull(const WINPR_JSON* item)
173{
174 return cJSON_IsNull((const cJSON*)item);
175}
176
177BOOL WINPR_JSON_IsNumber(const WINPR_JSON* item)
178{
179 return cJSON_IsNumber((const cJSON*)item);
180}
181
182BOOL WINPR_JSON_IsString(const WINPR_JSON* item)
183{
184 return cJSON_IsString((const cJSON*)item);
185}
186
187BOOL WINPR_JSON_IsArray(const WINPR_JSON* item)
188{
189 return cJSON_IsArray((const cJSON*)item);
190}
191
192BOOL WINPR_JSON_IsObject(const WINPR_JSON* item)
193{
194 return cJSON_IsObject((const cJSON*)item);
195}
196
197WINPR_JSON* WINPR_JSON_CreateNull(void)
198{
199 return cJSON_CreateNull();
200}
201
202WINPR_JSON* WINPR_JSON_CreateTrue(void)
203{
204 return cJSON_CreateTrue();
205}
206
207WINPR_JSON* WINPR_JSON_CreateFalse(void)
208{
209 return cJSON_CreateFalse();
210}
211
212WINPR_JSON* WINPR_JSON_CreateBool(BOOL boolean)
213{
214 return cJSON_CreateBool(boolean);
215}
216
217WINPR_JSON* WINPR_JSON_CreateNumber(double num)
218{
219 return cJSON_CreateNumber(num);
220}
221
222WINPR_JSON* WINPR_JSON_CreateString(const char* string)
223{
224 return cJSON_CreateString(string);
225}
226
227WINPR_JSON* WINPR_JSON_CreateArray(void)
228{
229 return cJSON_CreateArray();
230}
231
232WINPR_JSON* WINPR_JSON_CreateObject(void)
233{
234 return cJSON_CreateObject();
235}
236
237WINPR_JSON* WINPR_JSON_AddNullToObject(WINPR_JSON* object, const char* name)
238{
239 return cJSON_AddNullToObject((cJSON*)object, name);
240}
241
242WINPR_JSON* WINPR_JSON_AddTrueToObject(WINPR_JSON* object, const char* name)
243{
244 return cJSON_AddTrueToObject((cJSON*)object, name);
245}
246
247WINPR_JSON* WINPR_JSON_AddFalseToObject(WINPR_JSON* object, const char* name)
248{
249 return cJSON_AddFalseToObject((cJSON*)object, name);
250}
251
252WINPR_JSON* WINPR_JSON_AddBoolToObject(WINPR_JSON* object, const char* name, BOOL boolean)
253{
254 return cJSON_AddBoolToObject((cJSON*)object, name, boolean);
255}
256
257WINPR_JSON* WINPR_JSON_AddNumberToObject(WINPR_JSON* object, const char* name, double number)
258{
259 return cJSON_AddNumberToObject((cJSON*)object, name, number);
260}
261
262WINPR_JSON* WINPR_JSON_AddStringToObject(WINPR_JSON* object, const char* name, const char* string)
263{
264 return cJSON_AddStringToObject((cJSON*)object, name, string);
265}
266
267WINPR_JSON* WINPR_JSON_AddObjectToObject(WINPR_JSON* object, const char* name)
268{
269 return cJSON_AddObjectToObject((cJSON*)object, name);
270}
271
272BOOL WINPR_JSON_AddItemToArray(WINPR_JSON* array, WINPR_JSON* item)
273{
274#if defined(USE_CJSON_COMPAT)
275 if ((array == NULL) || (item == NULL))
276 return FALSE;
277 cJSON_AddItemToArray((cJSON*)array, (cJSON*)item);
278 return TRUE;
279#else
280 return cJSON_AddItemToArray((cJSON*)array, (cJSON*)item);
281#endif
282}
283
284WINPR_JSON* WINPR_JSON_AddArrayToObject(WINPR_JSON* object, const char* name)
285{
286 return cJSON_AddArrayToObject((cJSON*)object, name);
287}
288
289char* WINPR_JSON_Print(WINPR_JSON* item)
290{
291 return cJSON_Print((const cJSON*)item);
292}
293
294char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item)
295{
296 return cJSON_PrintUnformatted((const cJSON*)item);
297}
WINPR_JSON * WINPR_JSON_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
Definition c-json.c:212
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
Definition c-json.c:222
BOOL WINPR_JSON_HasObjectItem(const WINPR_JSON *object, const char *string)
Check if JSON has an object matching the name.
Definition c-json.c:132
WINPR_JSON * WINPR_JSON_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
Definition c-json.c:257
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
Definition c-json.c:172
WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition c-json.c:122
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:182
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
Definition c-json.c:272
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
Definition c-json.c:284
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition c-json.c:167
double WINPR_JSON_GetNumberValue(const WINPR_JSON *item)
Return the Number value of a JSON item.
Definition c-json.c:147
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
Definition c-json.c:242
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
Definition c-json.c:232
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
Definition c-json.c:227
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
Definition c-json.c:88
char * WINPR_JSON_Print(WINPR_JSON *item)
Serialize a JSON instance to string for minimal size without formatting see WINPR_JSON_PrintUnformatt...
Definition c-json.c:289
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
Definition c-json.c:247
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition c-json.c:177
WINPR_JSON * WINPR_JSON_GetArrayItem(const WINPR_JSON *array, size_t index)
Return a pointer to an item in the array.
Definition c-json.c:108
WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive(const WINPR_JSON *object, const char *string)
Same as WINPR_JSON_GetObjectItem but with case sensitive matching.
Definition c-json.c:127
WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
Definition c-json.c:262
WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
Definition c-json.c:98
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
Definition c-json.c:207
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
Definition c-json.c:217
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition c-json.c:192
WINPR_JSON * WINPR_JSON_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
Definition c-json.c:252
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *item)
Check if JSON item is valid.
Definition c-json.c:152
char * WINPR_JSON_PrintUnformatted(WINPR_JSON *item)
Serialize a JSON instance to string without formatting for human readable formatted output see WINPR_...
Definition c-json.c:294
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
Definition c-json.c:197
const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
Definition c-json.c:237
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
Definition c-json.c:202
BOOL WINPR_JSON_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
Definition c-json.c:157
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition c-json.c:114
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition c-json.c:187
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
Definition c-json.c:137
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
Definition c-json.c:267
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition c-json.c:93
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition c-json.c:162