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 4.5 kB

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