FreeRDP
Loading...
Searching...
No Matches
WinPR JSON wrapper

Wrapper around cJSON or JSONC libraries. More...

Typedefs

typedef void WINPR_JSON
 

Functions

WINPR_API int WINPR_JSON_version (char *buffer, size_t len)
 Get the library version string.
 
WINPR_API void WINPR_JSON_Delete (WINPR_JSON *item)
 Delete a WinPR JSON wrapper object.
 
WINPR_API WINPR_JSON * WINPR_JSON_Parse (const char *value)
 Parse a '\0' terminated JSON string.
 
WINPR_API WINPR_JSON * WINPR_JSON_ParseWithLength (const char *value, size_t buffer_length)
 Parse a JSON string.
 
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile (const char *filename)
 Parse a JSON string read from a file filename.
 
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFileFP (FILE *fp)
 Parse a JSON string read from a FILE.
 
WINPR_API size_t WINPR_JSON_GetArraySize (const WINPR_JSON *array)
 Get the number of arrayitems from an array.
 
WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem (const WINPR_JSON *array, size_t index)
 Return a pointer to an item in the array.
 
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItem (const WINPR_JSON *object, const char *string)
 Return a pointer to an JSON object item.
 
WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive (const WINPR_JSON *object, const char *string)
 Same as WINPR_JSON_GetObjectItem but with case insensitive matching.
 
WINPR_API BOOL WINPR_JSON_HasObjectItem (const WINPR_JSON *object, const char *string)
 Check if JSON has an object matching the name.
 
WINPR_API const char * WINPR_JSON_GetErrorPtr (void)
 Return an error string.
 
WINPR_API const char * WINPR_JSON_GetStringValue (WINPR_JSON *item)
 Return the String value of a JSON item.
 
WINPR_API double WINPR_JSON_GetNumberValue (const WINPR_JSON *item)
 Return the Number value of a JSON item.
 
WINPR_API BOOL WINPR_JSON_IsInvalid (const WINPR_JSON *item)
 Check if JSON item is valid.
 
WINPR_API BOOL WINPR_JSON_IsFalse (const WINPR_JSON *item)
 Check if JSON item is BOOL value False.
 
WINPR_API BOOL WINPR_JSON_IsTrue (const WINPR_JSON *item)
 Check if JSON item is BOOL value True.
 
WINPR_API BOOL WINPR_JSON_IsBool (const WINPR_JSON *item)
 Check if JSON item is of type BOOL.
 
WINPR_API BOOL WINPR_JSON_IsNull (const WINPR_JSON *item)
 Check if JSON item is Null.
 
WINPR_API BOOL WINPR_JSON_IsNumber (const WINPR_JSON *item)
 Check if JSON item is of type Number.
 
WINPR_API BOOL WINPR_JSON_IsString (const WINPR_JSON *item)
 Check if JSON item is of type String.
 
WINPR_API BOOL WINPR_JSON_IsArray (const WINPR_JSON *item)
 Check if JSON item is of type Array.
 
WINPR_API BOOL WINPR_JSON_IsObject (const WINPR_JSON *item)
 Check if JSON item is of type Object.
 
WINPR_API WINPR_JSON * WINPR_JSON_CreateNull (void)
 WINPR_JSON_CreateNull.
 
WINPR_API WINPR_JSON * WINPR_JSON_CreateTrue (void)
 WINPR_JSON_CreateTrue.
 
WINPR_API WINPR_JSON * WINPR_JSON_CreateFalse (void)
 WINPR_JSON_CreateFalse.
 
WINPR_API WINPR_JSON * WINPR_JSON_CreateBool (BOOL boolean)
 WINPR_JSON_CreateBool.
 
WINPR_API WINPR_JSON * WINPR_JSON_CreateNumber (double num)
 WINPR_JSON_CreateNumber.
 
WINPR_API WINPR_JSON * WINPR_JSON_CreateString (const char *string)
 WINPR_JSON_CreateString.
 
WINPR_API WINPR_JSON * WINPR_JSON_CreateArray (void)
 WINPR_JSON_CreateArray.
 
WINPR_API WINPR_JSON * WINPR_JSON_CreateObject (void)
 WINPR_JSON_CreateObject.
 
WINPR_API WINPR_JSON * WINPR_JSON_AddNullToObject (WINPR_JSON *object, const char *name)
 WINPR_JSON_AddNullToObject.
 
WINPR_API WINPR_JSON * WINPR_JSON_AddTrueToObject (WINPR_JSON *object, const char *name)
 WINPR_JSON_AddTrueToObject.
 
WINPR_API WINPR_JSON * WINPR_JSON_AddFalseToObject (WINPR_JSON *object, const char *name)
 WINPR_JSON_AddFalseToObject.
 
WINPR_API WINPR_JSON * WINPR_JSON_AddBoolToObject (WINPR_JSON *object, const char *name, BOOL boolean)
 WINPR_JSON_AddBoolToObject.
 
WINPR_API WINPR_JSON * WINPR_JSON_AddNumberToObject (WINPR_JSON *object, const char *name, double number)
 WINPR_JSON_AddNumberToObject.
 
WINPR_API WINPR_JSON * WINPR_JSON_AddStringToObject (WINPR_JSON *object, const char *name, const char *string)
 WINPR_JSON_AddStringToObject.
 
WINPR_API WINPR_JSON * WINPR_JSON_AddObjectToObject (WINPR_JSON *object, const char *name)
 WINPR_JSON_AddObjectToObject.
 
WINPR_API WINPR_JSON * WINPR_JSON_AddArrayToObject (WINPR_JSON *object, const char *name)
 WINPR_JSON_AddArrayToObject.
 
WINPR_API BOOL WINPR_JSON_AddItemToArray (WINPR_JSON *array, WINPR_JSON *item)
 Add an item to an existing array.
 
WINPR_API char * WINPR_JSON_Print (WINPR_JSON *item)
 Serialize a JSON instance to string for minimal size without formatting see WINPR_JSON_PrintUnformatted.
 
WINPR_API char * WINPR_JSON_PrintUnformatted (WINPR_JSON *item)
 Serialize a JSON instance to string without formatting for human readable formatted output see WINPR_JSON_Print.
 

Detailed Description

Wrapper around cJSON or JSONC libraries.

FreeRDP: A Remote Desktop Protocol Implementation JSON parser wrapper

Copyright 2024 Armin Novak anova.nosp@m.k@th.nosp@m.incas.nosp@m.t.co.nosp@m.m Copyright 2024 Thincast Technologies GmbH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Since
version 3.6.0

Typedef Documentation

◆ WINPR_JSON

typedef void WINPR_JSON

Definition at line 39 of file json.h.

Function Documentation

◆ WINPR_JSON_AddArrayToObject()

WINPR_API WINPR_JSON * WINPR_JSON_AddArrayToObject ( WINPR_JSON *  object,
const char *  name 
)

WINPR_JSON_AddArrayToObject.

Parameters
objectThe JSON object the new item is added to
nameThe name of the object
Returns
the new JSON item added
Since
version 3.6.0

Definition at line 635 of file json.c.

636{
637#if defined(WITH_JSONC)
638 struct json_object* obj = json_object_new_array();
639 if (json_object_object_add((json_object*)object, name, obj) != 0)
640 {
641 json_object_put(obj);
642 return NULL;
643 }
644 return obj;
645#elif defined(WITH_CJSON)
646 return cJSON_AddArrayToObject((cJSON*)object, name);
647#else
648 WINPR_UNUSED(object);
649 WINPR_UNUSED(name);
650 return NULL;
651#endif
652}

◆ WINPR_JSON_AddBoolToObject()

WINPR_API WINPR_JSON * WINPR_JSON_AddBoolToObject ( WINPR_JSON *  object,
const char *  name,
BOOL  boolean 
)

WINPR_JSON_AddBoolToObject.

Parameters
objectThe JSON object the new item is added to
nameThe name of the object
Returns
the new JSON item added
Since
version 3.6.0

Definition at line 533 of file json.c.

534{
535#if defined(WITH_JSONC)
536 struct json_object* obj = json_object_new_boolean(boolean);
537 if (json_object_object_add((json_object*)object, name, obj) != 0)
538 {
539 json_object_put(obj);
540 return NULL;
541 }
542 return obj;
543#elif defined(WITH_CJSON)
544 return cJSON_AddBoolToObject((cJSON*)object, name, boolean);
545#else
546 WINPR_UNUSED(object);
547 WINPR_UNUSED(name);
548 WINPR_UNUSED(boolean);
549 return NULL;
550#endif
551}

Referenced by freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_AddFalseToObject()

WINPR_API WINPR_JSON * WINPR_JSON_AddFalseToObject ( WINPR_JSON *  object,
const char *  name 
)

WINPR_JSON_AddFalseToObject.

Parameters
objectThe JSON object the new item is added to
nameThe name of the object
Returns
the new JSON item added
Since
version 3.6.0

Definition at line 514 of file json.c.

515{
516#if defined(WITH_JSONC)
517 struct json_object* obj = json_object_new_boolean(FALSE);
518 if (json_object_object_add((json_object*)object, name, obj) != 0)
519 {
520 json_object_put(obj);
521 return NULL;
522 }
523 return obj;
524#elif defined(WITH_CJSON)
525 return cJSON_AddFalseToObject((cJSON*)object, name);
526#else
527 WINPR_UNUSED(object);
528 WINPR_UNUSED(name);
529 return NULL;
530#endif
531}

◆ WINPR_JSON_AddItemToArray()

WINPR_API BOOL WINPR_JSON_AddItemToArray ( WINPR_JSON *  array,
WINPR_JSON *  item 
)

Add an item to an existing array.

Parameters
arrayAn array to add to, must not be NULL
itemAn item to add, must not be NULL
Returns
TRUE for success, FALSE for failure
Since
version 3.7.0

Definition at line 612 of file json.c.

613{
614#if defined(WITH_JSONC)
615 const int rc = json_object_array_add((json_object*)array, (json_object*)item);
616 if (rc != 0)
617 return FALSE;
618 return TRUE;
619#elif defined(WITH_CJSON)
620#if defined(USE_CJSON_COMPAT)
621 if ((array == NULL) || (item == NULL))
622 return FALSE;
623 cJSON_AddItemToArray((cJSON*)array, (cJSON*)item);
624 return TRUE;
625#else
626 return cJSON_AddItemToArray((cJSON*)array, (cJSON*)item);
627#endif
628#else
629 WINPR_UNUSED(array);
630 WINPR_UNUSED(item);
631 return FALSE;
632#endif
633}

◆ WINPR_JSON_AddNullToObject()

WINPR_API WINPR_JSON * WINPR_JSON_AddNullToObject ( WINPR_JSON *  object,
const char *  name 
)

WINPR_JSON_AddNullToObject.

Parameters
objectThe JSON object the new item is added to
nameThe name of the object
Returns
the new JSON item added
Since
version 3.6.0

Definition at line 476 of file json.c.

477{
478#if defined(WITH_JSONC)
479 struct json_object* obj = json_object_new_null();
480 if (json_object_object_add((json_object*)object, name, obj) != 0)
481 {
482 json_object_put(obj);
483 return NULL;
484 }
485 return obj;
486#elif defined(WITH_CJSON)
487 return cJSON_AddNullToObject((cJSON*)object, name);
488#else
489 WINPR_UNUSED(object);
490 WINPR_UNUSED(name);
491 return NULL;
492#endif
493}

Referenced by freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_AddNumberToObject()

WINPR_API WINPR_JSON * WINPR_JSON_AddNumberToObject ( WINPR_JSON *  object,
const char *  name,
double  number 
)

WINPR_JSON_AddNumberToObject.

Parameters
objectThe JSON object the new item is added to
nameThe name of the object
Returns
the new JSON item added
Since
version 3.6.0

Definition at line 553 of file json.c.

554{
555#if defined(WITH_JSONC)
556 struct json_object* obj = json_object_new_double(number);
557 if (json_object_object_add((json_object*)object, name, obj) != 0)
558 {
559 json_object_put(obj);
560 return NULL;
561 }
562 return obj;
563#elif defined(WITH_CJSON)
564 return cJSON_AddNumberToObject((cJSON*)object, name, number);
565#else
566 WINPR_UNUSED(object);
567 WINPR_UNUSED(name);
568 WINPR_UNUSED(number);
569 return NULL;
570#endif
571}

Referenced by freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_AddObjectToObject()

WINPR_API WINPR_JSON * WINPR_JSON_AddObjectToObject ( WINPR_JSON *  object,
const char *  name 
)

WINPR_JSON_AddObjectToObject.

Parameters
objectThe JSON object the new item is added to
nameThe name of the object
Returns
the new JSON item added
Since
version 3.6.0

Definition at line 593 of file json.c.

594{
595#if defined(WITH_JSONC)
596 struct json_object* obj = json_object_new_object();
597 if (json_object_object_add((json_object*)object, name, obj) != 0)
598 {
599 json_object_put(obj);
600 return NULL;
601 }
602 return obj;
603#elif defined(WITH_CJSON)
604 return cJSON_AddObjectToObject((cJSON*)object, name);
605#else
606 WINPR_UNUSED(object);
607 WINPR_UNUSED(name);
608 return NULL;
609#endif
610}

Referenced by freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_AddStringToObject()

WINPR_API WINPR_JSON * WINPR_JSON_AddStringToObject ( WINPR_JSON *  object,
const char *  name,
const char *  string 
)

WINPR_JSON_AddStringToObject.

Parameters
objectThe JSON object the new item is added to
nameThe name of the object
Returns
the new JSON item added
Since
version 3.6.0

Definition at line 573 of file json.c.

574{
575#if defined(WITH_JSONC)
576 struct json_object* obj = json_object_new_string(string);
577 if (json_object_object_add((json_object*)object, name, obj) != 0)
578 {
579 json_object_put(obj);
580 return NULL;
581 }
582 return obj;
583#elif defined(WITH_CJSON)
584 return cJSON_AddStringToObject((cJSON*)object, name, string);
585#else
586 WINPR_UNUSED(object);
587 WINPR_UNUSED(name);
588 WINPR_UNUSED(string);
589 return NULL;
590#endif
591}

Referenced by freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_AddTrueToObject()

WINPR_API WINPR_JSON * WINPR_JSON_AddTrueToObject ( WINPR_JSON *  object,
const char *  name 
)

WINPR_JSON_AddTrueToObject.

Parameters
objectThe JSON object the new item is added to
nameThe name of the object
Returns
the new JSON item added
Since
version 3.6.0

Definition at line 495 of file json.c.

496{
497#if defined(WITH_JSONC)
498 struct json_object* obj = json_object_new_boolean(TRUE);
499 if (json_object_object_add((json_object*)object, name, obj) != 0)
500 {
501 json_object_put(obj);
502 return NULL;
503 }
504 return obj;
505#elif defined(WITH_CJSON)
506 return cJSON_AddTrueToObject((cJSON*)object, name);
507#else
508 WINPR_UNUSED(object);
509 WINPR_UNUSED(name);
510 return NULL;
511#endif
512}

◆ WINPR_JSON_CreateArray()

WINPR_API WINPR_JSON * WINPR_JSON_CreateArray ( void  )

WINPR_JSON_CreateArray.

Returns
a new JSON item of type array, empty
Since
version 3.6.0

Definition at line 454 of file json.c.

455{
456#if defined(WITH_JSONC)
457 return json_object_new_array();
458#elif defined(WITH_CJSON)
459 return cJSON_CreateArray();
460#else
461 return NULL;
462#endif
463}

◆ WINPR_JSON_CreateBool()

WINPR_API WINPR_JSON * WINPR_JSON_CreateBool ( BOOL  boolean)

WINPR_JSON_CreateBool.

Parameters
booleanthe value the JSON item should have
Returns
a new JSON item of type Bool
Since
version 3.6.0

Definition at line 418 of file json.c.

419{
420#if defined(WITH_JSONC)
421 return json_object_new_boolean(boolean);
422#elif defined(WITH_CJSON)
423 return cJSON_CreateBool(boolean);
424#else
425 WINPR_UNUSED(boolean);
426 return NULL;
427#endif
428}

◆ WINPR_JSON_CreateFalse()

WINPR_API WINPR_JSON * WINPR_JSON_CreateFalse ( void  )

WINPR_JSON_CreateFalse.

Returns
a new JSON item of type Bool and value False
Since
version 3.6.0

Definition at line 407 of file json.c.

408{
409#if defined(WITH_JSONC)
410 return json_object_new_boolean(FALSE);
411#elif defined(WITH_CJSON)
412 return cJSON_CreateFalse();
413#else
414 return NULL;
415#endif
416}

◆ WINPR_JSON_CreateNull()

WINPR_API WINPR_JSON * WINPR_JSON_CreateNull ( void  )

WINPR_JSON_CreateNull.

Returns
a new JSON item of type and value Null
Since
version 3.6.0

Definition at line 385 of file json.c.

386{
387#if defined(WITH_JSONC)
388 return json_object_new_null();
389#elif defined(WITH_CJSON)
390 return cJSON_CreateNull();
391#else
392 return NULL;
393#endif
394}

◆ WINPR_JSON_CreateNumber()

WINPR_API WINPR_JSON * WINPR_JSON_CreateNumber ( double  num)

WINPR_JSON_CreateNumber.

Parameters
numthe number value of the new item
Returns
a new JSON item of type Number
Since
version 3.6.0

Definition at line 430 of file json.c.

431{
432#if defined(WITH_JSONC)
433 return json_object_new_double(num);
434#elif defined(WITH_CJSON)
435 return cJSON_CreateNumber(num);
436#else
437 WINPR_UNUSED(num);
438 return NULL;
439#endif
440}

◆ WINPR_JSON_CreateObject()

WINPR_API WINPR_JSON * WINPR_JSON_CreateObject ( void  )

WINPR_JSON_CreateObject.

Returns
a new JSON item of type Object
Since
version 3.6.0

Definition at line 465 of file json.c.

466{
467#if defined(WITH_JSONC)
468 return json_object_new_object();
469#elif defined(WITH_CJSON)
470 return cJSON_CreateObject();
471#else
472 return NULL;
473#endif
474}

Referenced by freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_CreateString()

WINPR_API WINPR_JSON * WINPR_JSON_CreateString ( const char *  string)

WINPR_JSON_CreateString.

Parameters
stringThe string value of the new item
Returns
a new JSON item of type String
Since
version 3.6.0

Definition at line 442 of file json.c.

443{
444#if defined(WITH_JSONC)
445 return json_object_new_string(string);
446#elif defined(WITH_CJSON)
447 return cJSON_CreateString(string);
448#else
449 WINPR_UNUSED(string);
450 return NULL;
451#endif
452}

◆ WINPR_JSON_CreateTrue()

WINPR_API WINPR_JSON * WINPR_JSON_CreateTrue ( void  )

WINPR_JSON_CreateTrue.

Returns
a new JSON item of type Bool and value True
Since
version 3.6.0

Definition at line 396 of file json.c.

397{
398#if defined(WITH_JSONC)
399 return json_object_new_boolean(TRUE);
400#elif defined(WITH_CJSON)
401 return cJSON_CreateTrue();
402#else
403 return NULL;
404#endif
405}

◆ WINPR_JSON_Delete()

WINPR_API void WINPR_JSON_Delete ( WINPR_JSON *  item)

Delete a WinPR JSON wrapper object.

Parameters
itemThe instance to delete
Since
version 3.6.0

Definition at line 144 of file json.c.

145{
146#if defined(WITH_JSONC)
147 json_object_put((json_object*)item);
148#elif defined(WITH_CJSON)
149 cJSON_Delete((cJSON*)item);
150#else
151 WINPR_UNUSED(item);
152#endif
153}

Referenced by freerdp_settings_deserialize(), and freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_GetArrayItem()

WINPR_API WINPR_JSON * WINPR_JSON_GetArrayItem ( const WINPR_JSON *  array,
size_t  index 
)

Return a pointer to an item in the array.

Parameters
arraythe JSON instance to query
indexThe index of the array item
Returns
A pointer to the array item or NULL if failed
Since
version 3.6.0

Definition at line 155 of file json.c.

156{
157#if defined(WITH_JSONC)
158 return json_object_array_get_idx((const json_object*)array, index);
159#elif defined(WITH_CJSON)
160 WINPR_ASSERT(index <= INT_MAX);
161 return cJSON_GetArrayItem((const cJSON*)array, (INT)index);
162#else
163 WINPR_UNUSED(array);
164 WINPR_UNUSED(index);
165 return NULL;
166#endif
167}

◆ WINPR_JSON_GetArraySize()

WINPR_API size_t WINPR_JSON_GetArraySize ( const WINPR_JSON *  array)

Get the number of arrayitems from an array.

Parameters
arraythe JSON instance to query
Returns
number of array items
Since
version 3.6.0

Definition at line 169 of file json.c.

170{
171#if defined(WITH_JSONC)
172 return json_object_array_length((const json_object*)array);
173#elif defined(WITH_CJSON)
174 const int rc = cJSON_GetArraySize((const cJSON*)array);
175 if (rc <= 0)
176 return 0;
177 return (size_t)rc;
178#else
179 WINPR_UNUSED(array);
180 return 0;
181#endif
182}

◆ WINPR_JSON_GetErrorPtr()

WINPR_API const char * WINPR_JSON_GetErrorPtr ( void  )

Return an error string.

Returns
A string describing the last error that occurred or NULL
Since
version 3.6.0

Definition at line 223 of file json.c.

224{
225#if defined(WITH_JSONC)
226 return json_util_get_last_err();
227#elif defined(WITH_CJSON)
228 return cJSON_GetErrorPtr();
229#else
230 return NULL;
231#endif
232}

◆ WINPR_JSON_GetNumberValue()

WINPR_API double WINPR_JSON_GetNumberValue ( const WINPR_JSON *  item)

Return the Number value of a JSON item.

Parameters
itemthe JSON item to query
Returns
The Number value or NaN if failed
Since
version 3.6.0

Definition at line 246 of file json.c.

247{
248#if defined(WITH_JSONC)
249 return json_object_get_double((const json_object*)item);
250#elif defined(WITH_CJSON)
251 return cJSON_GetNumberValue((const cJSON*)item);
252#else
253 WINPR_UNUSED(item);
254 return nan("");
255#endif
256}

◆ WINPR_JSON_GetObjectItem()

WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItem ( const WINPR_JSON *  object,
const char *  string 
)

Return a pointer to an JSON object item.

Parameters
objectthe JSON object
stringthe name of the object
Returns
A pointer to the object identified by string or NULL
Since
version 3.6.0

Definition at line 184 of file json.c.

185{
186#if defined(WITH_JSONC)
187 return json_object_object_get((const json_object*)object, string);
188#elif defined(WITH_CJSON)
189 return cJSON_GetObjectItem((const cJSON*)object, string);
190#else
191 WINPR_UNUSED(object);
192 WINPR_UNUSED(string);
193 return NULL;
194#endif
195}

Referenced by freerdp_settings_deserialize().

Here is the caller graph for this function:

◆ WINPR_JSON_GetObjectItemCaseSensitive()

WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItemCaseSensitive ( const WINPR_JSON *  object,
const char *  string 
)

Same as WINPR_JSON_GetObjectItem but with case insensitive matching.

Parameters
objectthe JSON instance to query
stringthe name of the object
Returns
A pointer to the object identified by string or NULL
Since
version 3.6.0

Definition at line 197 of file json.c.

198{
199#if defined(WITH_JSONC)
200 return json_object_object_get((const json_object*)object, string);
201#elif defined(WITH_CJSON)
202 return cJSON_GetObjectItemCaseSensitive((const cJSON*)object, string);
203#else
204 WINPR_UNUSED(object);
205 WINPR_UNUSED(string);
206 return NULL;
207#endif
208}

◆ WINPR_JSON_GetStringValue()

WINPR_API const char * WINPR_JSON_GetStringValue ( WINPR_JSON *  item)

Return the String value of a JSON item.

Parameters
itemthe JSON item to query
Returns
The string value or NULL if failed
Since
version 3.6.0

Definition at line 234 of file json.c.

235{
236#if defined(WITH_JSONC)
237 return json_object_get_string((json_object*)item);
238#elif defined(WITH_CJSON)
239 return cJSON_GetStringValue((cJSON*)item);
240#else
241 WINPR_UNUSED(item);
242 return NULL;
243#endif
244}

Referenced by freerdp_settings_deserialize().

Here is the caller graph for this function:

◆ WINPR_JSON_HasObjectItem()

WINPR_API BOOL WINPR_JSON_HasObjectItem ( const WINPR_JSON *  object,
const char *  string 
)

Check if JSON has an object matching the name.

Parameters
objectthe JSON instance
stringthe name of the object
Returns
TRUE if found, FALSE otherwise
Since
version 3.6.0

Definition at line 210 of file json.c.

211{
212#if defined(WITH_JSONC)
213 return json_object_object_get_ex((const json_object*)object, string, NULL);
214#elif defined(WITH_CJSON)
215 return cJSON_HasObjectItem((const cJSON*)object, string);
216#else
217 WINPR_UNUSED(object);
218 WINPR_UNUSED(string);
219 return FALSE;
220#endif
221}

◆ WINPR_JSON_IsArray()

WINPR_API BOOL WINPR_JSON_IsArray ( const WINPR_JSON *  item)

Check if JSON item is of type Array.

Parameters
itemthe JSON item to query
Returns
TRUE if the type is Array, FALSE otherwise
Since
version 3.6.0

Definition at line 361 of file json.c.

362{
363#if defined(WITH_JSONC)
364 return json_object_is_type((const json_object*)item, json_type_array);
365#elif defined(WITH_CJSON)
366 return cJSON_IsArray((const cJSON*)item);
367#else
368 WINPR_UNUSED(item);
369 return FALSE;
370#endif
371}

Referenced by WINPR_JSON_IsInvalid().

Here is the caller graph for this function:

◆ WINPR_JSON_IsBool()

WINPR_API BOOL WINPR_JSON_IsBool ( const WINPR_JSON *  item)

Check if JSON item is of type BOOL.

Parameters
itemthe JSON item to query
Returns
TRUE if the type is BOOL, FALSE otherwise
Since
version 3.6.0

Definition at line 312 of file json.c.

313{
314#if defined(WITH_JSONC)
315 return json_object_is_type((const json_object*)item, json_type_boolean);
316#elif defined(WITH_CJSON)
317 return cJSON_IsBool((const cJSON*)item);
318#else
319 WINPR_UNUSED(item);
320 return FALSE;
321#endif
322}

Referenced by freerdp_settings_deserialize(), and WINPR_JSON_IsInvalid().

Here is the caller graph for this function:

◆ WINPR_JSON_IsFalse()

WINPR_API BOOL WINPR_JSON_IsFalse ( const WINPR_JSON *  item)

Check if JSON item is BOOL value False.

Parameters
itemthe JSON item to query
Returns
TRUE if False, FALSE otherwise
Since
version 3.6.0

Definition at line 282 of file json.c.

283{
284#if defined(WITH_JSONC)
285 if (!json_object_is_type((const json_object*)item, json_type_boolean))
286 return FALSE;
287 json_bool val = json_object_get_boolean((const json_object*)item);
288 return val == 0;
289#elif defined(WITH_CJSON)
290 return cJSON_IsFalse((const cJSON*)item);
291#else
292 WINPR_UNUSED(item);
293 return FALSE;
294#endif
295}

◆ WINPR_JSON_IsInvalid()

WINPR_API BOOL WINPR_JSON_IsInvalid ( const WINPR_JSON *  item)

Check if JSON item is valid.

Parameters
itemthe JSON item to query
Returns
TRUE if valid, FALSE otherwise
Since
version 3.6.0

Definition at line 258 of file json.c.

259{
260#if defined(WITH_JSONC)
261 if (WINPR_JSON_IsArray(item))
262 return FALSE;
263 if (WINPR_JSON_IsObject(item))
264 return FALSE;
265 if (WINPR_JSON_IsNull(item))
266 return FALSE;
267 if (WINPR_JSON_IsNumber(item))
268 return FALSE;
269 if (WINPR_JSON_IsBool(item))
270 return FALSE;
271 if (WINPR_JSON_IsString(item))
272 return FALSE;
273 return TRUE;
274#elif defined(WITH_CJSON)
275 return cJSON_IsInvalid((const cJSON*)item);
276#else
277 WINPR_UNUSED(item);
278 return TRUE;
279#endif
280}
BOOL WINPR_JSON_IsNull(const WINPR_JSON *item)
Check if JSON item is Null.
Definition json.c:324
BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition json.c:349
BOOL WINPR_JSON_IsBool(const WINPR_JSON *item)
Check if JSON item is of type BOOL.
Definition json.c:312
BOOL WINPR_JSON_IsNumber(const WINPR_JSON *item)
Check if JSON item is of type Number.
Definition json.c:336
BOOL WINPR_JSON_IsObject(const WINPR_JSON *item)
Check if JSON item is of type Object.
Definition json.c:373
BOOL WINPR_JSON_IsArray(const WINPR_JSON *item)
Check if JSON item is of type Array.
Definition json.c:361

References WINPR_JSON_IsArray(), WINPR_JSON_IsBool(), WINPR_JSON_IsNull(), WINPR_JSON_IsNumber(), WINPR_JSON_IsObject(), and WINPR_JSON_IsString().

Here is the call graph for this function:

◆ WINPR_JSON_IsNull()

WINPR_API BOOL WINPR_JSON_IsNull ( const WINPR_JSON *  item)

Check if JSON item is Null.

Parameters
itemthe JSON item to query
Returns
TRUE if it is Null, FALSE otherwise
Since
version 3.6.0

Definition at line 324 of file json.c.

325{
326#if defined(WITH_JSONC)
327 return json_object_is_type((const json_object*)item, json_type_null);
328#elif defined(WITH_CJSON)
329 return cJSON_IsNull((const cJSON*)item);
330#else
331 WINPR_UNUSED(item);
332 return FALSE;
333#endif
334}

Referenced by freerdp_settings_deserialize(), and WINPR_JSON_IsInvalid().

Here is the caller graph for this function:

◆ WINPR_JSON_IsNumber()

WINPR_API BOOL WINPR_JSON_IsNumber ( const WINPR_JSON *  item)

Check if JSON item is of type Number.

Parameters
itemthe JSON item to query
Returns
TRUE if the type is Number, FALSE otherwise
Since
version 3.6.0

Definition at line 336 of file json.c.

337{
338#if defined(WITH_JSONC)
339 return json_object_is_type((const json_object*)item, json_type_int) ||
340 json_object_is_type((const json_object*)item, json_type_double);
341#elif defined(WITH_CJSON)
342 return cJSON_IsNumber((const cJSON*)item);
343#else
344 WINPR_UNUSED(item);
345 return FALSE;
346#endif
347}

Referenced by WINPR_JSON_IsInvalid().

Here is the caller graph for this function:

◆ WINPR_JSON_IsObject()

WINPR_API BOOL WINPR_JSON_IsObject ( const WINPR_JSON *  item)

Check if JSON item is of type Object.

Parameters
itemthe JSON item to query
Returns
TRUE if the type is Object, FALSE otherwise
Since
version 3.6.0

Definition at line 373 of file json.c.

374{
375#if defined(WITH_JSONC)
376 return json_object_is_type((const json_object*)item, json_type_object);
377#elif defined(WITH_CJSON)
378 return cJSON_IsObject((const cJSON*)item);
379#else
380 WINPR_UNUSED(item);
381 return FALSE;
382#endif
383}

Referenced by WINPR_JSON_IsInvalid().

Here is the caller graph for this function:

◆ WINPR_JSON_IsString()

WINPR_API BOOL WINPR_JSON_IsString ( const WINPR_JSON *  item)

Check if JSON item is of type String.

Parameters
itemthe JSON item to query
Returns
TRUE if the type is String, FALSE otherwise
Since
version 3.6.0

Definition at line 349 of file json.c.

350{
351#if defined(WITH_JSONC)
352 return json_object_is_type((const json_object*)item, json_type_string);
353#elif defined(WITH_CJSON)
354 return cJSON_IsString((const cJSON*)item);
355#else
356 WINPR_UNUSED(item);
357 return FALSE;
358#endif
359}

Referenced by freerdp_settings_deserialize(), and WINPR_JSON_IsInvalid().

Here is the caller graph for this function:

◆ WINPR_JSON_IsTrue()

WINPR_API BOOL WINPR_JSON_IsTrue ( const WINPR_JSON *  item)

Check if JSON item is BOOL value True.

Parameters
itemthe JSON item to query
Returns
TRUE if True, FALSE otherwise
Since
version 3.6.0

Definition at line 297 of file json.c.

298{
299#if defined(WITH_JSONC)
300 if (!json_object_is_type((const json_object*)item, json_type_boolean))
301 return FALSE;
302 json_bool val = json_object_get_boolean((const json_object*)item);
303 return val != 0;
304#elif defined(WITH_CJSON)
305 return cJSON_IsTrue((const cJSON*)item);
306#else
307 WINPR_UNUSED(item);
308 return FALSE;
309#endif
310}

Referenced by freerdp_settings_deserialize().

Here is the caller graph for this function:

◆ WINPR_JSON_Parse()

WINPR_API WINPR_JSON * WINPR_JSON_Parse ( const char *  value)

Parse a '\0' terminated JSON string.

Parameters
valueA '\0' terminated JSON string
Returns
A WinPR JSON wrapper object holding the parsed string or NULL if failed
Since
version 3.6.0

Definition at line 113 of file json.c.

114{
115#if defined(WITH_JSONC)
116 return json_tokener_parse(value);
117#elif defined(WITH_CJSON)
118 return cJSON_Parse(value);
119#else
120 WINPR_UNUSED(value);
121 return NULL;
122#endif
123}

◆ WINPR_JSON_ParseFromFile()

WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile ( const char *  filename)

Parse a JSON string read from a file filename.

Parameters
filenamethe name of the file to read from
Returns
A WinPR JSON wrapper object holding the parsed string or NULL if failed
Since
version 3.16.0

Definition at line 684 of file json.c.

685{
686 FILE* fp = winpr_fopen(filename, "r");
687 if (!fp)
688 return NULL;
689 WINPR_JSON* json = WINPR_JSON_ParseFromFileFP(fp);
690 (void)fclose(fp);
691 return json;
692}
WINPR_JSON * WINPR_JSON_ParseFromFileFP(FILE *fp)
Parse a JSON string read from a FILE.
Definition json.c:694

References WINPR_JSON_ParseFromFileFP().

Here is the call graph for this function:

◆ WINPR_JSON_ParseFromFileFP()

WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFileFP ( FILE *  fp)

Parse a JSON string read from a FILE.

Parameters
fpa FILE pointer to read from.
Returns
A WinPR JSON wrapper object holding the parsed string or NULL if failed
Since
version 3.16.0

Definition at line 694 of file json.c.

695{
696 if (!fp)
697 return NULL;
698
699 if (fseek(fp, 0, SEEK_END) != 0)
700 return NULL;
701
702 const INT64 size = _ftelli64(fp);
703 if (size < 0)
704 return NULL;
705
706 if (fseek(fp, 0, SEEK_SET) != 0)
707 return NULL;
708
709 const size_t usize = WINPR_ASSERTING_INT_CAST(size_t, size);
710 char* str = calloc(usize + 1, sizeof(char));
711 if (!str)
712 return NULL;
713
714 WINPR_JSON* json = NULL;
715 const size_t s = fread(str, sizeof(char), usize, fp);
716 if (s == usize)
717 json = WINPR_JSON_ParseWithLength(str, usize);
718 free(str);
719 return json;
720}
WINPR_JSON * WINPR_JSON_ParseWithLength(const char *value, size_t buffer_length)
Parse a JSON string.
Definition json.c:125

References WINPR_JSON_ParseWithLength().

Referenced by WINPR_JSON_ParseFromFile().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WINPR_JSON_ParseWithLength()

WINPR_API WINPR_JSON * WINPR_JSON_ParseWithLength ( const char *  value,
size_t  buffer_length 
)

Parse a JSON string.

Parameters
valueA JSON string
buffer_lengthThe length in bytes of the JSON string
Returns
A WinPR JSON wrapper object holding the parsed string or NULL if failed
Since
version 3.6.0

Definition at line 125 of file json.c.

126{
127#if defined(WITH_JSONC)
128 WINPR_ASSERT(buffer_length <= INT_MAX);
129 json_tokener* tok = json_tokener_new();
130 if (!tok)
131 return NULL;
132 json_object* obj = json_tokener_parse_ex(tok, value, (int)buffer_length);
133 json_tokener_free(tok);
134 return obj;
135#elif defined(WITH_CJSON)
136 return cJSON_ParseWithLength(value, buffer_length);
137#else
138 WINPR_UNUSED(value);
139 WINPR_UNUSED(buffer_length);
140 return NULL;
141#endif
142}

Referenced by freerdp_settings_deserialize(), and WINPR_JSON_ParseFromFileFP().

Here is the caller graph for this function:

◆ WINPR_JSON_Print()

WINPR_API char * WINPR_JSON_Print ( WINPR_JSON *  item)

Serialize a JSON instance to string for minimal size without formatting see WINPR_JSON_PrintUnformatted.

Parameters
itemThe JSON instance to serialize
Returns
A string representation of the JSON instance or NULL
Since
version 3.6.0

Definition at line 654 of file json.c.

655{
656#if defined(WITH_JSONC)
657 const char* str = json_object_to_json_string_ext((json_object*)item, JSON_C_TO_STRING_PRETTY);
658 if (!str)
659 return NULL;
660 return _strdup(str);
661#elif defined(WITH_CJSON)
662 return cJSON_Print((const cJSON*)item);
663#else
664 WINPR_UNUSED(item);
665 return NULL;
666#endif
667}

Referenced by freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_PrintUnformatted()

WINPR_API char * WINPR_JSON_PrintUnformatted ( WINPR_JSON *  item)

Serialize a JSON instance to string without formatting for human readable formatted output see WINPR_JSON_Print.

Parameters
itemThe JSON instance to serialize
Returns
A string representation of the JSON instance or NULL
Since
version 3.6.0

Definition at line 669 of file json.c.

670{
671#if defined(WITH_JSONC)
672 const char* str = json_object_to_json_string_ext((json_object*)item, JSON_C_TO_STRING_PLAIN);
673 if (!str)
674 return NULL;
675 return _strdup(str);
676#elif defined(WITH_CJSON)
677 return cJSON_PrintUnformatted((const cJSON*)item);
678#else
679 WINPR_UNUSED(item);
680 return NULL;
681#endif
682}

Referenced by freerdp_settings_serialize().

Here is the caller graph for this function:

◆ WINPR_JSON_version()

WINPR_API int WINPR_JSON_version ( char *  buffer,
size_t  len 
)

Get the library version string.

Parameters
buffera string buffer to hold the version string
lenthe length of the buffer
Returns
length of the version string in bytes or negative for error
Since
version 3.6.0

FreeRDP: A Remote Desktop Protocol Implementation JSON parser wrapper

Copyright 2024 Armin Novak anova.nosp@m.k@th.nosp@m.incas.nosp@m.t.co.nosp@m.m Copyright 2024 Thincast Technologies GmbH

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Definition at line 101 of file json.c.

102{
103#if defined(WITH_JSONC)
104 return _snprintf(buffer, len, "json-c %s", json_c_version());
105#elif defined(WITH_CJSON)
106 return _snprintf(buffer, len, "cJSON %s", cJSON_Version());
107#else
108 (void)_snprintf(buffer, len, "JSON support not available");
109 return -1;
110#endif
111}