| 183 * Type alias for the JSON value struct. |
181 * Type alias for the JSON value struct. |
| 184 */ |
182 */ |
| 185 typedef struct cx_json_value_s CxJsonValue; |
183 typedef struct cx_json_value_s CxJsonValue; |
| 186 |
184 |
| 187 /** |
185 /** |
| 188 * Type alias for the JSON array struct. |
|
| 189 */ |
|
| 190 typedef struct cx_json_array_s CxJsonArray; |
|
| 191 /** |
|
| 192 * Type alias for the map representing a JSON object. |
186 * Type alias for the map representing a JSON object. |
| 193 * The map contains pointers of type @c CxJsonValue. |
187 * The map contains pointers of type @c CxJsonValue. |
| 194 */ |
188 */ |
| 195 typedef CxMap* CxJsonObject; |
189 typedef CxMap* CxJsonObject; |
| 196 /** |
190 /** |
| 207 typedef double CxJsonNumber; |
201 typedef double CxJsonNumber; |
| 208 /** |
202 /** |
| 209 * Type alias for a JSON literal. |
203 * Type alias for a JSON literal. |
| 210 */ |
204 */ |
| 211 typedef enum cx_json_literal CxJsonLiteral; |
205 typedef enum cx_json_literal CxJsonLiteral; |
| 212 |
|
| 213 /** |
|
| 214 * JSON array structure. |
|
| 215 */ |
|
| 216 struct cx_json_array_s { |
|
| 217 /** |
|
| 218 * The array data. |
|
| 219 */ |
|
| 220 CX_ARRAY_DECLARE(CxJsonValue*, data); |
|
| 221 }; |
|
| 222 |
206 |
| 223 /** |
207 /** |
| 224 * Structure for a JSON value. |
208 * Structure for a JSON value. |
| 225 */ |
209 */ |
| 226 struct cx_json_value_s { |
210 struct cx_json_value_s { |
| 325 cxmutstr uncompleted_member_name; |
310 cxmutstr uncompleted_member_name; |
| 326 |
311 |
| 327 /** |
312 /** |
| 328 * State stack. |
313 * State stack. |
| 329 */ |
314 */ |
| 330 CX_ARRAY_DECLARE_SIZED(int, states, unsigned); |
315 CX_ARRAY(int, states); |
| 331 |
316 |
| 332 /** |
317 /** |
| 333 * Value buffer stack. |
318 * Value buffer stack. |
| 334 */ |
319 */ |
| 335 CX_ARRAY_DECLARE_SIZED(CxJsonValue*, vbuf, unsigned); |
320 CX_ARRAY(CxJsonValue*, vbuf); |
| 336 |
321 |
| 337 /** |
322 /** |
| 338 * Internally reserved memory for the state stack. |
323 * Internally reserved memory for the state stack. |
| 339 */ |
324 */ |
| 340 int states_internal[8]; |
325 int states_internal[8]; |
| 475 |
460 |
| 476 |
461 |
| 477 /** |
462 /** |
| 478 * Produces a compact string representation of the specified JSON value. |
463 * Produces a compact string representation of the specified JSON value. |
| 479 * |
464 * |
| |
465 * @param allocator the allocator for the string |
| 480 * @param value the JSON value |
466 * @param value the JSON value |
| 481 * @param allocator the allocator for the string |
|
| 482 * @return the produced string |
467 * @return the produced string |
| 483 * @see cxJsonWrite() |
468 * @see cxJsonWrite() |
| 484 * @see cxJsonWriterCompact() |
469 * @see cxJsonWriterCompact() |
| 485 * @see cxJsonToPrettyString() |
470 * @see cxJsonToPrettyString() |
| 486 */ |
471 */ |
| 487 cx_attr_nonnull_arg(1) |
472 cx_attr_nonnull_arg(2) |
| 488 CX_EXPORT cxmutstr cxJsonToString(CxJsonValue *value, const CxAllocator *allocator); |
473 CX_EXPORT cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value); |
| 489 |
474 |
| 490 /** |
475 /** |
| 491 * Produces a pretty string representation of the specified JSON value. |
476 * Produces a pretty string representation of the specified JSON value. |
| 492 * |
477 * |
| |
478 * @param allocator the allocator for the string |
| 493 * @param value the JSON value |
479 * @param value the JSON value |
| 494 * @param allocator the allocator for the string |
|
| 495 * @return the produced string |
480 * @return the produced string |
| 496 * @see cxJsonWrite() |
481 * @see cxJsonWrite() |
| 497 * @see cxJsonWriterPretty() |
482 * @see cxJsonWriterPretty() |
| 498 * @see cxJsonToString() |
483 * @see cxJsonToString() |
| 499 */ |
484 */ |
| 500 cx_attr_nonnull_arg(1) |
485 cx_attr_nonnull_arg(2) |
| 501 CX_EXPORT cxmutstr cxJsonToPrettyString(CxJsonValue *value, const CxAllocator *allocator); |
486 CX_EXPORT cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value); |
| 502 |
487 |
| 503 /** |
488 /** |
| 504 * Initializes the JSON interface. |
489 * Initializes the JSON interface. |
| 505 * |
490 * |
| 506 * @param json the JSON interface |
491 * @param json the JSON interface |
| 626 CX_EXPORT CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator); |
611 CX_EXPORT CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator); |
| 627 |
612 |
| 628 /** |
613 /** |
| 629 * Creates a new (empty) JSON array. |
614 * Creates a new (empty) JSON array. |
| 630 * |
615 * |
| |
616 * Optionally, this function already allocates memory with the given capacity. |
| |
617 * |
| 631 * @param allocator the allocator to use |
618 * @param allocator the allocator to use |
| |
619 * @param capacity optional capacity or zero if it's unknown how many elements the array will have |
| 632 * @return the new JSON array or @c NULL if allocation fails |
620 * @return the new JSON array or @c NULL if allocation fails |
| 633 * @see cxJsonObjPutArr() |
621 * @see cxJsonObjPutArr() |
| 634 * @see cxJsonArrAddValues() |
622 * @see cxJsonArrAddValues() |
| 635 */ |
623 */ |
| 636 cx_attr_nodiscard |
624 cx_attr_nodiscard |
| 637 CX_EXPORT CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator); |
625 CX_EXPORT CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator, size_t capacity); |
| 638 |
626 |
| 639 /** |
627 /** |
| 640 * Creates a new JSON number value. |
628 * Creates a new JSON number value. |
| 641 * |
629 * |
| 642 * @param allocator the allocator to use |
630 * @param allocator the allocator to use |
| 838 * |
826 * |
| 839 * Internal function - use cxJsonObjPutArr(). |
827 * Internal function - use cxJsonObjPutArr(). |
| 840 * |
828 * |
| 841 * @param obj the target JSON object |
829 * @param obj the target JSON object |
| 842 * @param name the name of the new value |
830 * @param name the name of the new value |
| |
831 * @param capacity optional initial capacity |
| 843 * @return the new value or @c NULL if allocation fails |
832 * @return the new value or @c NULL if allocation fails |
| 844 * @see cxJsonObjPut() |
833 * @see cxJsonObjPut() |
| 845 * @see cxJsonCreateArr() |
834 * @see cxJsonCreateArr() |
| 846 */ |
835 */ |
| 847 cx_attr_nonnull |
836 cx_attr_nonnull |
| 848 CX_EXPORT CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name); |
837 CX_EXPORT CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name, size_t capacity); |
| 849 |
838 |
| 850 /** |
839 /** |
| 851 * Creates a new JSON array and adds it to an object. |
840 * Creates a new JSON array and adds it to an object. |
| 852 * |
841 * |
| 853 * @param obj (@c CxJsonValue*) the target JSON object |
842 * @param obj (@c CxJsonValue*) the target JSON object |
| 854 * @param name (any string) the name of the new value |
843 * @param name (any string) the name of the new value |
| |
844 * @param capacity (@c size_t) optional initial capacity |
| 855 * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails |
845 * @return (@c CxJsonValue*) the new value or @c NULL if allocation fails |
| 856 * @see cxJsonObjPut() |
846 * @see cxJsonObjPut() |
| 857 * @see cxJsonCreateArr() |
847 * @see cxJsonCreateArr() |
| 858 */ |
848 */ |
| 859 #define cxJsonObjPutArr(obj, name) cx_json_obj_put_arr(obj, cx_strcast(name)) |
849 #define cxJsonObjPutArr(obj, name, capacity) cx_json_obj_put_arr(obj, cx_strcast(name), capacity) |
| 860 |
850 |
| 861 /** |
851 /** |
| 862 * Creates a new JSON number and adds it to an object. |
852 * Creates a new JSON number and adds it to an object. |
| 863 * |
853 * |
| 864 * Internal function - use cxJsonObjPutNumber(). |
854 * Internal function - use cxJsonObjPutNumber(). |
| 1364 * @return the value corresponding to the key or @c NULL when the key is not part of the object |
1354 * @return the value corresponding to the key or @c NULL when the key is not part of the object |
| 1365 * @see cxJsonIsObject() |
1355 * @see cxJsonIsObject() |
| 1366 */ |
1356 */ |
| 1367 #define cxJsonObjRemove(value, name) cx_json_obj_remove(value, cx_strcast(name)) |
1357 #define cxJsonObjRemove(value, name) cx_json_obj_remove(value, cx_strcast(name)) |
| 1368 |
1358 |
| |
1359 /** |
| |
1360 * Performs a deep comparison of two JSON values. |
| |
1361 * |
| |
1362 * The order of object members is ignored during comparison. |
| |
1363 * |
| |
1364 * @param json the JSON value |
| |
1365 * @param other the other JSON value that the JSON value is compared to |
| |
1366 * @retval zero the values are equal (except for ordering of object members) |
| |
1367 * @retval non-zero the values differ |
| |
1368 */ |
| |
1369 CX_EXPORT int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other); |
| |
1370 |
| |
1371 |
| |
1372 /** |
| |
1373 * Creates a deep copy of the specified JSON value. |
| |
1374 * |
| |
1375 * If you need a @c cx_clone_func compatible version, see cxJsonCloneFunc(). |
| |
1376 * |
| |
1377 * @note when you are cloning @c NULL, you will get a pointer to a statically |
| |
1378 * allocated value which represents nothing. |
| |
1379 * |
| |
1380 * @param value the value to be cloned |
| |
1381 * @param allocator the allocator for the new value |
| |
1382 * @return the new value or @c NULL if any allocation was unsuccessful |
| |
1383 * @see cxJsonCloneFunc() |
| |
1384 */ |
| |
1385 cx_attr_nodiscard |
| |
1386 CX_EXPORT CxJsonValue* cxJsonClone(const CxJsonValue* value, |
| |
1387 const CxAllocator* allocator); |
| |
1388 |
| |
1389 |
| |
1390 /** |
| |
1391 * A @c cx_clone_func compatible version of cxJsonClone(). |
| |
1392 * |
| |
1393 * Internal function - use cxJsonCloneFunc() to get a properly casted function pointer. |
| |
1394 * |
| |
1395 * @param target the target memory or @c NULL |
| |
1396 * @param source the value to be cloned |
| |
1397 * @param allocator the allocator for the new value |
| |
1398 * @param data unused |
| |
1399 * @return the new value or @c NULL if any allocation was unsuccessful |
| |
1400 * @see cxJsonClone() |
| |
1401 */ |
| |
1402 cx_attr_nodiscard |
| |
1403 CX_EXPORT CxJsonValue* cx_json_clone_func( |
| |
1404 CxJsonValue* target, const CxJsonValue* source, |
| |
1405 const CxAllocator* allocator, void *data); |
| |
1406 |
| |
1407 /** |
| |
1408 * A @c cx_clone_func compatible version of cxJsonClone(). |
| |
1409 * |
| |
1410 * @param target (@c CxJsonValue*) the target memory or @c NULL |
| |
1411 * @param source (@c CxJsonValue*) the value to be cloned |
| |
1412 * @param allocator (@c CxAllocator*) the allocator for the new value |
| |
1413 * @param data unused |
| |
1414 * @return the new value or @c NULL if any allocation was unsuccessful |
| |
1415 * @see cxJsonClone() |
| |
1416 */ |
| |
1417 #define cxJsonCloneFunc ((cx_clone_func) cx_json_clone_func) |
| |
1418 |
| 1369 #ifdef __cplusplus |
1419 #ifdef __cplusplus |
| 1370 } |
1420 } |
| 1371 #endif |
1421 #endif |
| 1372 |
1422 |
| 1373 #endif /* UCX_JSON_H */ |
1423 #endif /* UCX_JSON_H */ |