src/cx/json.h

changeset 1037
83620ba72cc1
parent 1033
e3009345984b
equal deleted inserted replaced
1036:6d29532a76c3 1037:83620ba72cc1
234 /** 234 /**
235 * Structure for a key/value entry in a JSON object. 235 * Structure for a key/value entry in a JSON object.
236 */ 236 */
237 struct cx_json_obj_value_s { 237 struct cx_json_obj_value_s {
238 /** 238 /**
239 * The key a.k.a. name of the value. 239 * The key (or name in JSON terminology) of the value.
240 */ 240 */
241 char *name; 241 cxmutstr name;
242 /** 242 /**
243 * The value. 243 * The value.
244 */ 244 */
245 CxJsonValue *value; 245 CxJsonValue *value;
246 }; 246 };
466 int cxJsonFilln(CxJson *json, const char *buf, size_t len); 466 int cxJsonFilln(CxJson *json, const char *buf, size_t len);
467 467
468 #ifdef __cplusplus 468 #ifdef __cplusplus
469 } // extern "C" 469 } // extern "C"
470 470
471 /**
472 * Adds more data to the input buffer.
473 *
474 * The data will be copied.
475 *
476 * @param json the json interface
477 * @param str the string to add to the buffer
478 * @return zero on success, non-zero on internal allocation error
479 * @see cxJsonFilln()
480 */
481 cx_attr_nonnull 471 cx_attr_nonnull
482 static inline int cxJsonFill( 472 static inline int cxJsonFill(
483 CxJson *json, 473 CxJson *json,
484 cxstring str 474 cxstring str
485 ) { 475 ) {
486 return cxJsonFilln(json, str.ptr, str.length); 476 return cxJsonFilln(json, str.ptr, str.length);
487 } 477 }
488 478
489 /**
490 * Adds more data to the input buffer.
491 *
492 * The data will be copied.
493 *
494 * @param json the json interface
495 * @param str the string to add to the buffer
496 * @return zero on success, non-zero on internal allocation error
497 * @see cxJsonFilln()
498 */
499 cx_attr_nonnull 479 cx_attr_nonnull
500 static inline int cxJsonFill( 480 static inline int cxJsonFill(
501 CxJson *json, 481 CxJson *json,
502 cxmutstr str 482 cxmutstr str
503 ) { 483 ) {
504 return cxJsonFilln(json, str.ptr, str.length); 484 return cxJsonFilln(json, str.ptr, str.length);
505 } 485 }
506 486
507 /**
508 * Adds more data to the input buffer.
509 *
510 * The data will be copied.
511 *
512 * @param json the json interface
513 * @param str the string to add to the buffer
514 * @return zero on success, non-zero on internal allocation error
515 * @see cxJsonFilln()
516 */
517 cx_attr_nonnull 487 cx_attr_nonnull
518 cx_attr_cstr_arg(2) 488 cx_attr_cstr_arg(2)
519 static inline int cxJsonFill( 489 static inline int cxJsonFill(
520 CxJson *json, 490 CxJson *json,
521 const char *str 491 const char *str
541 char*: cx_json_fill_str, \ 511 char*: cx_json_fill_str, \
542 const char*: cx_json_fill_str) \ 512 const char*: cx_json_fill_str) \
543 (json, str) 513 (json, str)
544 514
545 /** 515 /**
546 * Adds more data to the input buffer. 516 * @copydoc cxJsonFill()
547 *
548 * Internal function - please use the #cxJsonFill() macro.
549 *
550 * @param json the json interface
551 * @param str the string to add to the buffer
552 * @return zero on success, non-zero on internal allocation error
553 * @see cxJsonFill()
554 */ 517 */
555 cx_attr_nonnull 518 cx_attr_nonnull
556 static inline int cx_json_fill_cxstr( 519 static inline int cx_json_fill_cxstr(
557 CxJson *json, 520 CxJson *json,
558 cxstring str 521 cxstring str
559 ) { 522 ) {
560 return cxJsonFilln(json, str.ptr, str.length); 523 return cxJsonFilln(json, str.ptr, str.length);
561 } 524 }
562 525
563 /** 526 /**
564 * Adds more data to the input buffer. 527 * @copydoc cxJsonFill()
565 *
566 * Internal function - please use the #cxJsonFill() macro.
567 *
568 * @param json the json interface
569 * @param str the string to add to the buffer
570 * @return zero on success, non-zero on internal allocation error
571 * @see cxJsonFill()
572 */ 528 */
573 cx_attr_nonnull 529 cx_attr_nonnull
574 static inline int cx_json_fill_mutstr( 530 static inline int cx_json_fill_mutstr(
575 CxJson *json, 531 CxJson *json,
576 cxmutstr str 532 cxmutstr str
577 ) { 533 ) {
578 return cxJsonFilln(json, str.ptr, str.length); 534 return cxJsonFilln(json, str.ptr, str.length);
579 } 535 }
580 536
581 /** 537 /**
582 * Adds more data to the input buffer. 538 * @copydoc cxJsonFill()
583 *
584 * Internal function - please use the #cxJsonFill() macro.
585 *
586 * @param json the json interface
587 * @param str the string to add to the buffer
588 * @return zero on success, non-zero on internal allocation error
589 * @see cxJsonFill()
590 */ 539 */
591 cx_attr_nonnull 540 cx_attr_nonnull
592 cx_attr_cstr_arg(2) 541 cx_attr_cstr_arg(2)
593 static inline int cx_json_fill_str( 542 static inline int cx_json_fill_str(
594 CxJson *json, 543 CxJson *json,
899 cx_attr_nonnull 848 cx_attr_nonnull
900 cx_attr_nodiscard 849 cx_attr_nodiscard
901 CxIterator cxJsonArrIter(const CxJsonValue *value); 850 CxIterator cxJsonArrIter(const CxJsonValue *value);
902 851
903 /** 852 /**
853 * @copydoc cxJsonObjGet()
854 */
855 cx_attr_nonnull
856 cx_attr_returns_nonnull
857 CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name);
858
859 #ifdef __cplusplus
860 } // extern "C"
861
862 CxJsonValue *cxJsonObjGet(const CxJsonValue *value, cxstring name) {
863 return cx_json_obj_get_cxstr(value, name);
864 }
865
866 CxJsonValue *cxJsonObjGet(const CxJsonValue *value, cxmutstr name) {
867 return cx_json_obj_get_cxstr(value, cx_strcast(name));
868 }
869
870 CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char *name) {
871 return cx_json_obj_get_cxstr(value, cx_str(name));
872 }
873
874 extern "C" {
875 #else
876 /**
904 * Returns a value corresponding to a key in a JSON object. 877 * Returns a value corresponding to a key in a JSON object.
905 * 878 *
906 * If the \p value is not a JSON object, the behavior is undefined. 879 * If the \p value is not a JSON object, the behavior is undefined.
907 * 880 *
908 * This function guarantees to return a JSON value. If the 881 * This function guarantees to return a JSON value. If the
912 * @param value the JSON object 885 * @param value the JSON object
913 * @param name the key to look up 886 * @param name the key to look up
914 * @return the value corresponding to the key 887 * @return the value corresponding to the key
915 * @see cxJsonIsObject() 888 * @see cxJsonIsObject()
916 */ 889 */
890 #define cxJsonObjGet(value, name) _Generic((name), \
891 cxstring: cx_json_obj_get_cxstr, \
892 cxmutstr: cx_json_obj_get_mutstr, \
893 char*: cx_json_obj_get_str, \
894 const char*: cx_json_obj_get_str) \
895 (value, name)
896
897 /**
898 * @copydoc cxJsonObjGet()
899 */
900 cx_attr_nonnull
901 cx_attr_returns_nonnull
902 static inline CxJsonValue *cx_json_obj_get_mutstr(const CxJsonValue *value, cxmutstr name) {
903 return cx_json_obj_get_cxstr(value, cx_strcast(name));
904 }
905
906 /**
907 * @copydoc cxJsonObjGet()
908 */
917 cx_attr_nonnull 909 cx_attr_nonnull
918 cx_attr_returns_nonnull 910 cx_attr_returns_nonnull
919 cx_attr_cstr_arg(2) 911 cx_attr_cstr_arg(2)
920 CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char* name); 912 static inline CxJsonValue *cx_json_obj_get_str(const CxJsonValue *value, const char *name) {
913 return cx_json_obj_get_cxstr(value, cx_str(name));
914 }
915 #endif
921 916
922 #ifdef __cplusplus 917 #ifdef __cplusplus
923 } 918 }
924 #endif 919 #endif
925 920

mercurial