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

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