FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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 633 of file json.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Referenced by 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 280 of file json.c.

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

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

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

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

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

Referenced by 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 334 of file json.c.

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

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

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

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

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

Referenced by 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 295 of file json.c.

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

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

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

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

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

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

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

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

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

◆ 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 return _snprintf(buffer, len, "JSON support not available");
108#endif
109}