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 = nullptr;
61 double dval = strtod(val, &endptr);
62 if (val == endptr)
63 return NAN;
64 if (endptr != nullptr)
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 nullptr;
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
152INT64 WINPR_JSON_GetIntegerValue(const WINPR_JSON* item)
153{
154 const double d = WINPR_JSON_GetNumberValue(item);
155 return WINPR_CXX_COMPAT_CAST(INT64, d);
156}
157
158UINT64 WINPR_JSON_GetUIntegerValue(const WINPR_JSON* item)
159{
160 const double d = WINPR_JSON_GetNumberValue(item);
161 return WINPR_CXX_COMPAT_CAST(UINT64, d);
162}
163
164BOOL WINPR_JSON_IsInvalid(const WINPR_JSON* item)
165{
166 return cJSON_IsInvalid((const cJSON*)item);
167}
168
169BOOL WINPR_JSON_IsFalse(const WINPR_JSON* item)
170{
171 return cJSON_IsFalse((const cJSON*)item);
172}
173
174BOOL WINPR_JSON_IsTrue(const WINPR_JSON* item)
175{
176 return cJSON_IsTrue((const cJSON*)item);
177}
178
179BOOL WINPR_JSON_IsBool(const WINPR_JSON* item)
180{
181 return cJSON_IsBool((const cJSON*)item);
182}
183
184BOOL WINPR_JSON_IsNull(const WINPR_JSON* item)
185{
186 return cJSON_IsNull((const cJSON*)item);
187}
188
189BOOL WINPR_JSON_IsNumber(const WINPR_JSON* item)
190{
191 return cJSON_IsNumber((const cJSON*)item);
192}
193
194BOOL WINPR_JSON_IsString(const WINPR_JSON* item)
195{
196 return cJSON_IsString((const cJSON*)item);
197}
198
199BOOL WINPR_JSON_IsArray(const WINPR_JSON* item)
200{
201 return cJSON_IsArray((const cJSON*)item);
202}
203
204BOOL WINPR_JSON_IsObject(const WINPR_JSON* item)
205{
206 return cJSON_IsObject((const cJSON*)item);
207}
208
209WINPR_JSON* WINPR_JSON_CreateNull(void)
210{
211 return cJSON_CreateNull();
212}
213
214WINPR_JSON* WINPR_JSON_CreateTrue(void)
215{
216 return cJSON_CreateTrue();
217}
218
219WINPR_JSON* WINPR_JSON_CreateFalse(void)
220{
221 return cJSON_CreateFalse();
222}
223
224WINPR_JSON* WINPR_JSON_CreateBool(BOOL boolean)
225{
226 return cJSON_CreateBool(boolean);
227}
228
229WINPR_JSON* WINPR_JSON_CreateNumber(double num)
230{
231 return cJSON_CreateNumber(num);
232}
233
234WINPR_JSON* WINPR_JSON_CreateString(const char* string)
235{
236 return cJSON_CreateString(string);
237}
238
239WINPR_JSON* WINPR_JSON_CreateArray(void)
240{
241 return cJSON_CreateArray();
242}
243
244WINPR_JSON* WINPR_JSON_CreateObject(void)
245{
246 return cJSON_CreateObject();
247}
248
249WINPR_JSON* WINPR_JSON_AddNullToObject(WINPR_JSON* object, const char* name)
250{
251 return cJSON_AddNullToObject((cJSON*)object, name);
252}
253
254WINPR_JSON* WINPR_JSON_AddTrueToObject(WINPR_JSON* object, const char* name)
255{
256 return cJSON_AddTrueToObject((cJSON*)object, name);
257}
258
259WINPR_JSON* WINPR_JSON_AddFalseToObject(WINPR_JSON* object, const char* name)
260{
261 return cJSON_AddFalseToObject((cJSON*)object, name);
262}
263
264WINPR_JSON* WINPR_JSON_AddBoolToObject(WINPR_JSON* object, const char* name, BOOL boolean)
265{
266 return cJSON_AddBoolToObject((cJSON*)object, name, boolean);
267}
268
269WINPR_JSON* WINPR_JSON_AddNumberToObject(WINPR_JSON* object, const char* name, double number)
270{
271 return cJSON_AddNumberToObject((cJSON*)object, name, number);
272}
273
274WINPR_JSON* WINPR_JSON_AddIntegerToObject(WINPR_JSON* object, const char* name, int64_t number)
275{
276 char str[64] = WINPR_C_ARRAY_INIT;
277 (void)_snprintf(str, sizeof(str), "%" PRId64, number);
278 return cJSON_AddRawToObject((cJSON*)object, name, str);
279}
280
281WINPR_JSON* WINPR_JSON_AddUIntegerToObject(WINPR_JSON* object, const char* name, uint64_t number)
282{
283 char str[64] = WINPR_C_ARRAY_INIT;
284 (void)_snprintf(str, sizeof(str), "%" PRIu64, number);
285 return cJSON_AddRawToObject((cJSON*)object, name, str);
286}
287
288WINPR_JSON* WINPR_JSON_AddStringToObject(WINPR_JSON* object, const char* name, const char* string)
289{
290 return cJSON_AddStringToObject((cJSON*)object, name, string);
291}
292
293WINPR_JSON* WINPR_JSON_AddObjectToObject(WINPR_JSON* object, const char* name)
294{
295 return cJSON_AddObjectToObject((cJSON*)object, name);
296}
297
298BOOL WINPR_JSON_AddItemToArray(WINPR_JSON* array, WINPR_JSON* item)
299{
300#if defined(USE_CJSON_COMPAT)
301 if ((array == nullptr) || (item == nullptr))
302 return FALSE;
303 cJSON_AddItemToArray((cJSON*)array, (cJSON*)item);
304 return TRUE;
305#else
306 return cJSON_AddItemToArray((cJSON*)array, (cJSON*)item);
307#endif
308}
309
310WINPR_JSON* WINPR_JSON_AddArrayToObject(WINPR_JSON* object, const char* name)
311{
312 return cJSON_AddArrayToObject((cJSON*)object, name);
313}
314
315WINPR_ATTR_MALLOC(free, 1)
316static char* returnCopy(char* str)
317{
318 if (!str)
319 return nullptr;
320 char* copy = _strdup(str);
321 cJSON_free(str);
322 return copy;
323}
324
325char* WINPR_JSON_Print(WINPR_JSON* item)
326{
327 return returnCopy(cJSON_Print((const cJSON*)item));
328}
329
330char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item)
331{
332 return returnCopy(cJSON_PrintUnformatted((const cJSON*)item));
333}
WINPR_JSON * WINPR_JSON_CreateString(const char *string)
WINPR_JSON_CreateString.
Definition c-json.c:234
WINPR_JSON * WINPR_JSON_CreateNull(void)
WINPR_JSON_CreateNull.
Definition c-json.c:209
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
Definition c-json.c:184
WINPR_JSON * WINPR_JSON_CreateTrue(void)
WINPR_JSON_CreateTrue.
Definition c-json.c:214
WINPR_JSON * WINPR_JSON_CreateNumber(double num)
WINPR_JSON_CreateNumber.
Definition c-json.c:229
WINPR_JSON * WINPR_JSON_AddObjectToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddObjectToObject.
Definition c-json.c:293
WINPR_JSON * WINPR_JSON_CreateObject(void)
WINPR_JSON_CreateObject.
Definition c-json.c:244
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
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition c-json.c:179
BOOL WINPR_JSON_AddItemToArray(WINPR_JSON *array, WINPR_JSON *item)
Add an item to an existing array.
Definition c-json.c:298
UINT64 WINPR_JSON_GetUIntegerValue(const WINPR_JSON *item)
Return the unsigned Integer value of a JSON item.
Definition c-json.c:158
BOOL WINPR_JSON_IsInvalid(const WINPR_JSON *item)
Check if JSON item is valid.
Definition c-json.c:164
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition c-json.c:189
int WINPR_JSON_version(char *buffer, size_t len)
Get the library version string.
Definition c-json.c:88
size_t WINPR_JSON_GetArraySize(const WINPR_JSON *array)
Get the number of arrayitems from an array.
Definition c-json.c:114
WINPR_JSON * WINPR_JSON_CreateFalse(void)
WINPR_JSON_CreateFalse.
Definition c-json.c:219
WINPR_JSON * WINPR_JSON_AddIntegerToObject(WINPR_JSON *object, const char *name, int64_t number)
WINPR_JSON_AddIntegerToObject.
Definition c-json.c:274
WINPR_JSON * WINPR_JSON_AddFalseToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddFalseToObject.
Definition c-json.c:259
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:325
BOOL WINPR_JSON_IsTrue(const WINPR_JSON *item)
Check if JSON item is BOOL value True.
Definition c-json.c:174
WINPR_JSON * WINPR_JSON_AddTrueToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddTrueToObject.
Definition c-json.c:254
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_AddNumberToObject(WINPR_JSON *object, const char *name, double number)
WINPR_JSON_AddNumberToObject.
Definition c-json.c:269
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_IsFalse(const WINPR_JSON *item)
Check if JSON item is BOOL value False.
Definition c-json.c:169
INT64 WINPR_JSON_GetIntegerValue(const WINPR_JSON *item)
Return the Integer value of a JSON item.
Definition c-json.c:152
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_AddUIntegerToObject(WINPR_JSON *object, const char *name, uint64_t number)
WINPR_JSON_AddUIntegerToObject.
Definition c-json.c:281
const char * WINPR_JSON_GetErrorPtr(void)
Return an error string.
Definition c-json.c:137
WINPR_JSON * WINPR_JSON_AddArrayToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddArrayToObject.
Definition c-json.c:310
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:194
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
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:330
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_CreateBool(BOOL boolean)
WINPR_JSON_CreateBool.
Definition c-json.c:224
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition c-json.c:199
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition c-json.c:204
WINPR_JSON * WINPR_JSON_AddNullToObject(WINPR_JSON *object, const char *name)
WINPR_JSON_AddNullToObject.
Definition c-json.c:249
WINPR_JSON * WINPR_JSON_AddStringToObject(WINPR_JSON *object, const char *name, const char *string)
WINPR_JSON_AddStringToObject.
Definition c-json.c:288
void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
WINPR_JSON * WINPR_JSON_CreateArray(void)
WINPR_JSON_CreateArray.
Definition c-json.c:239
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_AddBoolToObject(WINPR_JSON *object, const char *name, BOOL boolean)
WINPR_JSON_AddBoolToObject.
Definition c-json.c:264
WINPR_JSON * WINPR_JSON_Parse(const char *value)
Parse a '\0' terminated JSON string.
Definition c-json.c:93