src/cx/json.h

changeset 1417
b97faf8b7ab7
parent 1338
cc153bffea28
equal deleted inserted replaced
1416:e67caa21d5a7 1417:b97faf8b7ab7
573 cx_attr_nonnull 573 cx_attr_nonnull
574 cx_attr_access_r(2, 3) 574 cx_attr_access_r(2, 3)
575 cx_attr_export 575 cx_attr_export
576 int cxJsonFilln(CxJson *json, const char *buf, size_t len); 576 int cxJsonFilln(CxJson *json, const char *buf, size_t len);
577 577
578 #ifdef __cplusplus 578
579 } // extern "C" 579 /**
580 580 * Internal function, do not use.
581 cx_attr_nonnull 581 *
582 static inline int cxJsonFill( 582 * @param json the json interface
583 CxJson *json, 583 * @param str the string
584 cxstring str 584 * @retval zero success
585 ) { 585 * @retval non-zero internal allocation error
586 */
587 cx_attr_nonnull
588 static inline int cx_json_fill(CxJson *json, cxstring str) {
586 return cxJsonFilln(json, str.ptr, str.length); 589 return cxJsonFilln(json, str.ptr, str.length);
587 } 590 }
588 591
589 cx_attr_nonnull
590 static inline int cxJsonFill(
591 CxJson *json,
592 cxmutstr str
593 ) {
594 return cxJsonFilln(json, str.ptr, str.length);
595 }
596
597 cx_attr_nonnull
598 cx_attr_cstr_arg(2)
599 static inline int cxJsonFill(
600 CxJson *json,
601 const char *str
602 ) {
603 return cxJsonFilln(json, str, strlen(str));
604 }
605
606 extern "C" {
607 #else // __cplusplus
608 /** 592 /**
609 * Fills the input buffer. 593 * Fills the input buffer.
610 * 594 *
611 * The JSON interface tries to avoid copying the input data. 595 * The JSON interface tries to avoid copying the input data.
612 * When you use this function and cxJsonNext() interleaving, 596 * When you use this function and cxJsonNext() interleaving,
620 * @param str the source string 604 * @param str the source string
621 * @retval zero success 605 * @retval zero success
622 * @retval non-zero internal allocation error 606 * @retval non-zero internal allocation error
623 * @see cxJsonFilln() 607 * @see cxJsonFilln()
624 */ 608 */
625 #define cxJsonFill(json, str) _Generic((str), \ 609 #define cxJsonFill(json, str) cx_json_fill(json, cx_strcast(str))
626 cxstring: cx_json_fill_cxstr, \
627 cxmutstr: cx_json_fill_mutstr, \
628 char*: cx_json_fill_str, \
629 const char*: cx_json_fill_str) \
630 (json, str)
631
632 /**
633 * @copydoc cxJsonFill()
634 */
635 cx_attr_nonnull
636 static inline int cx_json_fill_cxstr(
637 CxJson *json,
638 cxstring str
639 ) {
640 return cxJsonFilln(json, str.ptr, str.length);
641 }
642
643 /**
644 * @copydoc cxJsonFill()
645 */
646 cx_attr_nonnull
647 static inline int cx_json_fill_mutstr(
648 CxJson *json,
649 cxmutstr str
650 ) {
651 return cxJsonFilln(json, str.ptr, str.length);
652 }
653
654 /**
655 * @copydoc cxJsonFill()
656 */
657 cx_attr_nonnull
658 cx_attr_cstr_arg(2)
659 static inline int cx_json_fill_str(
660 CxJson *json,
661 const char *str
662 ) {
663 return cxJsonFilln(json, str, strlen(str));
664 }
665 #endif
666 610
667 /** 611 /**
668 * Creates a new (empty) JSON object. 612 * Creates a new (empty) JSON object.
669 * 613 *
670 * @param allocator the allocator to use 614 * @param allocator the allocator to use
1332 cx_attr_nodiscard 1276 cx_attr_nodiscard
1333 cx_attr_export 1277 cx_attr_export
1334 CxIterator cxJsonObjIter(const CxJsonValue *value); 1278 CxIterator cxJsonObjIter(const CxJsonValue *value);
1335 1279
1336 /** 1280 /**
1337 * @copydoc cxJsonObjGet() 1281 * Internal function, do not use.
1282 * @param value the JSON object
1283 * @param name the key to look up
1284 * @return the value corresponding to the key
1338 */ 1285 */
1339 cx_attr_nonnull 1286 cx_attr_nonnull
1340 cx_attr_returns_nonnull 1287 cx_attr_returns_nonnull
1341 cx_attr_export 1288 cx_attr_export
1342 CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name); 1289 CxJsonValue *cx_json_obj_get(const CxJsonValue *value, cxstring name);
1343 1290
1344 /**
1345 * @copydoc cxJsonObjRemove()
1346 */
1347 cx_attr_nonnull
1348 cx_attr_export
1349 CxJsonValue *cx_json_obj_remove_cxstr(CxJsonValue *value, cxstring name);
1350
1351 #ifdef __cplusplus
1352 } // extern "C"
1353
1354 static inline CxJsonValue *cxJsonObjGet(const CxJsonValue *value, cxstring name) {
1355 return cx_json_obj_get_cxstr(value, name);
1356 }
1357
1358 static inline CxJsonValue *cxJsonObjGet(const CxJsonValue *value, cxmutstr name) {
1359 return cx_json_obj_get_cxstr(value, cx_strcast(name));
1360 }
1361
1362 static inline CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char *name) {
1363 return cx_json_obj_get_cxstr(value, cx_str(name));
1364 }
1365
1366 static inline CxJsonValue *cxJsonObjRemove(CxJsonValue *value, cxstring name) {
1367 return cx_json_obj_remove_cxstr(value, name);
1368 }
1369
1370 static inline CxJsonValue *cxJsonObjRemove(CxJsonValue *value, cxmutstr name) {
1371 return cx_json_obj_remove_cxstr(value, cx_strcast(name));
1372 }
1373
1374 static inline CxJsonValue *cxJsonObjRemove(CxJsonValue *value, const char *name) {
1375 return cx_json_obj_remove_cxstr(value, cx_str(name));
1376 }
1377
1378 extern "C" {
1379 #else
1380 /** 1291 /**
1381 * Returns a value corresponding to a key in a JSON object. 1292 * Returns a value corresponding to a key in a JSON object.
1382 * 1293 *
1383 * If the @p value is not a JSON object, the behavior is undefined. 1294 * If the @p value is not a JSON object, the behavior is undefined.
1384 * 1295 *
1389 * @param value the JSON object 1300 * @param value the JSON object
1390 * @param name the key to look up 1301 * @param name the key to look up
1391 * @return the value corresponding to the key 1302 * @return the value corresponding to the key
1392 * @see cxJsonIsObject() 1303 * @see cxJsonIsObject()
1393 */ 1304 */
1394 #define cxJsonObjGet(value, name) _Generic((name), \ 1305 #define cxJsonObjGet(value, name) cx_json_obj_get(value, cx_strcast(name))
1395 cxstring: cx_json_obj_get_cxstr, \ 1306
1396 cxmutstr: cx_json_obj_get_mutstr, \ 1307 /**
1397 char*: cx_json_obj_get_str, \ 1308 * Internal function, do not use.
1398 const char*: cx_json_obj_get_str) \ 1309 * @param value the JSON object
1399 (value, name) 1310 * @param name the key to look up
1400 1311 * @return the value corresponding to the key or @c NULL when the key is not part of the object
1401 /** 1312 */
1402 * @copydoc cxJsonObjGet() 1313 cx_attr_nonnull
1403 */ 1314 cx_attr_export
1404 cx_attr_nonnull 1315 CxJsonValue *cx_json_obj_remove(CxJsonValue *value, cxstring name);
1405 cx_attr_returns_nonnull
1406 static inline CxJsonValue *cx_json_obj_get_mutstr(const CxJsonValue *value, cxmutstr name) {
1407 return cx_json_obj_get_cxstr(value, cx_strcast(name));
1408 }
1409
1410 /**
1411 * @copydoc cxJsonObjGet()
1412 */
1413 cx_attr_nonnull
1414 cx_attr_returns_nonnull
1415 cx_attr_cstr_arg(2)
1416 static inline CxJsonValue *cx_json_obj_get_str(const CxJsonValue *value, const char *name) {
1417 return cx_json_obj_get_cxstr(value, cx_str(name));
1418 }
1419 1316
1420 /** 1317 /**
1421 * Removes and returns a value corresponding to a key in a JSON object. 1318 * Removes and returns a value corresponding to a key in a JSON object.
1422 * 1319 *
1423 * If the @p value is not a JSON object, the behavior is undefined. 1320 * If the @p value is not a JSON object, the behavior is undefined.
1428 * @param value the JSON object 1325 * @param value the JSON object
1429 * @param name the key to look up 1326 * @param name the key to look up
1430 * @return the value corresponding to the key or @c NULL when the key is not part of the object 1327 * @return the value corresponding to the key or @c NULL when the key is not part of the object
1431 * @see cxJsonIsObject() 1328 * @see cxJsonIsObject()
1432 */ 1329 */
1433 #define cxJsonObjRemove(value, name) _Generic((name), \ 1330 #define cxJsonObjRemove(value, name) cx_json_obj_remove(value, cx_strcast(name))
1434 cxstring: cx_json_obj_remove_cxstr, \ 1331
1435 cxmutstr: cx_json_obj_remove_mutstr, \ 1332 #ifdef __cplusplus
1436 char*: cx_json_obj_remove_str, \
1437 const char*: cx_json_obj_remove_str) \
1438 (value, name)
1439
1440 /**
1441 * @copydoc cxJsonObjRemove()
1442 */
1443 cx_attr_nonnull
1444 static inline CxJsonValue *cx_json_obj_remove_mutstr(CxJsonValue *value, cxmutstr name) {
1445 return cx_json_obj_remove_cxstr(value, cx_strcast(name));
1446 }
1447
1448 /**
1449 * @copydoc cxJsonObjRemove()
1450 */
1451 cx_attr_nonnull
1452 cx_attr_cstr_arg(2)
1453 static inline CxJsonValue *cx_json_obj_remove_str(CxJsonValue *value, const char *name) {
1454 return cx_json_obj_remove_cxstr(value, cx_str(name));
1455 } 1333 }
1456 #endif 1334 #endif
1457 1335
1458 #ifdef __cplusplus
1459 }
1460 #endif
1461
1462 #endif /* UCX_JSON_H */ 1336 #endif /* UCX_JSON_H */
1463 1337

mercurial