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 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 634 of file json.c.

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

◆ 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 532 of file json.c.

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

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 513 of file json.c.

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

◆ 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 611 of file json.c.

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

◆ 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 475 of file json.c.

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

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 552 of file json.c.

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

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 592 of file json.c.

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

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 572 of file json.c.

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

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 494 of file json.c.

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

◆ 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 453 of file json.c.

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

◆ 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 417 of file json.c.

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

◆ 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 406 of file json.c.

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

◆ 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 384 of file json.c.

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

◆ 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 429 of file json.c.

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

◆ 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 464 of file json.c.

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

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 441 of file json.c.

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

◆ 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 395 of file json.c.

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

◆ 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 143 of file json.c.

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

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 154 of file json.c.

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

◆ 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 168 of file json.c.

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

◆ 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 222 of file json.c.

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

◆ 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 245 of file json.c.

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

◆ 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 183 of file json.c.

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

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 196 of file json.c.

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

◆ 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 233 of file json.c.

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

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 209 of file json.c.

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

◆ 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 360 of file json.c.

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

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 311 of file json.c.

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

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 281 of file json.c.

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

◆ 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 257 of file json.c.

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

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 323 of file json.c.

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

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 335 of file json.c.

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

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 372 of file json.c.

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

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 348 of file json.c.

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

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 296 of file json.c.

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

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 112 of file json.c.

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

◆ 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 124 of file json.c.

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

Referenced by freerdp_settings_deserialize().

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 653 of file json.c.

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

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 668 of file json.c.

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

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 100 of file json.c.

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