You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_pointer.h 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2016 Alexadru Ardelean.
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See COPYING for details.
  6. *
  7. */
  8. /**
  9. * @file
  10. * @brief JSON Pointer (RFC 6901) implementation for retrieving
  11. * objects from a json-c object tree.
  12. */
  13. #ifndef _json_pointer_h_
  14. #define _json_pointer_h_
  15. #include "json_object.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /**
  20. * Retrieves a JSON sub-object from inside another JSON object
  21. * using the JSON pointer notation as defined in RFC 6901
  22. * https://tools.ietf.org/html/rfc6901
  23. *
  24. * The returned JSON sub-object is equivalent to parsing manually the
  25. * 'obj' JSON tree ; i.e. it's not a new object that is created, but rather
  26. * a pointer inside the JSON tree.
  27. *
  28. * Internally, this is equivalent to doing a series of 'json_object_object_get()'
  29. * and 'json_object_array_get_idx()' along the given 'path'.
  30. *
  31. * Note that the 'path' string supports 'printf()' type arguments, so, whatever
  32. * is added after the 'res' param will be treated as an argument for 'path'
  33. * Example: json_pointer_get(obj, "/foo/%d/%s", &res, 0, bar)
  34. * This means, that you need to escape '%' with '%%' (just like in printf())
  35. *
  36. * @param obj the json_object instance/tree from where to retrieve sub-objects
  37. * @param path a (RFC6901) string notation for the sub-object to retrieve
  38. * @param res a pointer that stores a reference to the json_object
  39. * associated with the given path
  40. *
  41. * @return negative if an error (or not found), or 0 if succeeded
  42. */
  43. JSON_EXPORT int json_pointer_get(struct json_object *obj, const char *path,
  44. struct json_object **res);
  45. /**
  46. * This is a variant of 'json_pointer_get()' that supports printf() style arguments.
  47. *
  48. * Example: json_pointer_getf(obj, res, "/foo/%d/%s", 0, bak)
  49. * This also means that you need to escape '%' with '%%' (just like in printf())
  50. *
  51. * Please take into consideration all recommended 'printf()' format security
  52. * aspects when using this function.
  53. *
  54. * @param obj the json_object instance/tree to which to add a sub-object
  55. * @param res a pointer that stores a reference to the json_object
  56. * associated with the given path
  57. * @param path_fmt a printf() style format for the path
  58. *
  59. * @return negative if an error (or not found), or 0 if succeeded
  60. */
  61. JSON_EXPORT int json_pointer_getf(struct json_object *obj, struct json_object **res,
  62. const char *path_fmt, ...);
  63. /**
  64. * Sets JSON object 'value' in the 'obj' tree at the location specified
  65. * by the 'path'. 'path' is JSON pointer notation as defined in RFC 6901
  66. * https://tools.ietf.org/html/rfc6901
  67. *
  68. * Note that 'obj' is a double pointer, mostly for the "" (empty string)
  69. * case, where the entire JSON object would be replaced by 'value'.
  70. * In the case of the "" path, the object at '*obj' will have it's refcount
  71. * decremented with 'json_object_put()' and the 'value' object will be assigned to it.
  72. *
  73. * For other cases (JSON sub-objects) ownership of 'value' will be transferred into
  74. * '*obj' via 'json_object_object_add()' & 'json_object_array_put_idx()', so the
  75. * only time the refcount should be decremented for 'value' is when the return value of
  76. * 'json_pointer_set()' is negative (meaning the 'value' object did not get set into '*obj').
  77. *
  78. * That also implies that 'json_pointer_set()' does not do any refcount incrementing.
  79. * (Just that single decrement that was mentioned above).
  80. *
  81. * Note that the 'path' string supports 'printf()' type arguments, so, whatever
  82. * is added after the 'value' param will be treated as an argument for 'path'
  83. * Example: json_pointer_set(obj, "/foo/%d/%s", value, 0, bak)
  84. * This means, that you need to escape '%' with '%%' (just like in printf())
  85. *
  86. * @param obj the json_object instance/tree to which to add a sub-object
  87. * @param path a (RFC6901) string notation for the sub-object to set in the tree
  88. * @param value object to set at path
  89. *
  90. * @return negative if an error (or not found), or 0 if succeeded
  91. */
  92. JSON_EXPORT int json_pointer_set(struct json_object **obj, const char *path,
  93. struct json_object *value);
  94. /**
  95. * This is a variant of 'json_pointer_set()' that supports printf() style arguments.
  96. *
  97. * Example: json_pointer_setf(obj, value, "/foo/%d/%s", 0, bak)
  98. * This also means that you need to escape '%' with '%%' (just like in printf())
  99. *
  100. * Please take into consideration all recommended 'printf()' format security
  101. * aspects when using this function.
  102. *
  103. * @param obj the json_object instance/tree to which to add a sub-object
  104. * @param value object to set at path
  105. * @param path_fmt a printf() style format for the path
  106. *
  107. * @return negative if an error (or not found), or 0 if succeeded
  108. */
  109. JSON_EXPORT int json_pointer_setf(struct json_object **obj, struct json_object *value,
  110. const char *path_fmt, ...);
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #endif