json_pointer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 where to store 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. int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res);
  44. /**
  45. * This is a variant of 'json_pointer_get()' that supports printf() style arguments.
  46. *
  47. * Example: json_pointer_getf(obj, res, "/foo/%d/%s", 0, bak)
  48. * This also means that you need to escape '%' with '%%' (just like in printf())
  49. *
  50. * Please take into consideration all recommended 'printf()' format security
  51. * aspects when using this function.
  52. *
  53. * @param obj the json_object instance/tree to which to add a sub-object
  54. * @param res a pointer where to store a reference to the json_object
  55. * associated with the given path
  56. * @param path_fmt a printf() style format for the path
  57. *
  58. * @return negative if an error (or not found), or 0 if succeeded
  59. */
  60. int json_pointer_getf(struct json_object *obj, struct json_object **res, const char *path_fmt, ...);
  61. /**
  62. * Sets JSON object 'value' in the 'obj' tree at the location specified
  63. * by the 'path'. 'path' is JSON pointer notation as defined in RFC 6901
  64. * https://tools.ietf.org/html/rfc6901
  65. *
  66. * Note that 'obj' is a double pointer, mostly for the "" (empty string)
  67. * case, where the entire JSON object would be replaced by 'value'.
  68. * In the case of the "" path, the object at '*obj' will have it's refcount
  69. * decremented with 'json_object_put()' and the 'value' object will be assigned to it.
  70. *
  71. * For other cases (JSON sub-objects) ownership of 'value' will be transferred into
  72. * '*obj' via 'json_object_object_add()' & 'json_object_array_put_idx()', so the
  73. * only time the refcount should be decremented for 'value' is when the return value of
  74. * 'json_pointer_set()' is negative (meaning the 'value' object did not get set into '*obj').
  75. *
  76. * That also implies that 'json_pointer_set()' does not do any refcount incrementing.
  77. * (Just that single decrement that was mentioned above).
  78. *
  79. * Note that the 'path' string supports 'printf()' type arguments, so, whatever
  80. * is added after the 'value' param will be treated as an argument for 'path'
  81. * Example: json_pointer_set(obj, "/foo/%d/%s", value, 0, bak)
  82. * This means, that you need to escape '%' with '%%' (just like in printf())
  83. *
  84. * @param obj the json_object instance/tree to which to add a sub-object
  85. * @param path a (RFC6901) string notation for the sub-object to set in the tree
  86. * @param value object to set at path
  87. *
  88. * @return negative if an error (or not found), or 0 if succeeded
  89. */
  90. int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value);
  91. /**
  92. * This is a variant of 'json_pointer_set()' that supports printf() style arguments.
  93. *
  94. * Example: json_pointer_setf(obj, value, "/foo/%d/%s", 0, bak)
  95. * This also means that you need to escape '%' with '%%' (just like in printf())
  96. *
  97. * Please take into consideration all recommended 'printf()' format security
  98. * aspects when using this function.
  99. *
  100. * @param obj the json_object instance/tree to which to add a sub-object
  101. * @param value object to set at path
  102. * @param path_fmt a printf() style format for the path
  103. *
  104. * @return negative if an error (or not found), or 0 if succeeded
  105. */
  106. int json_pointer_setf(struct json_object **obj, struct json_object *value, const char *path_fmt, ...);
  107. #ifdef __cplusplus
  108. }
  109. #endif
  110. #endif