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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. * @param obj the json_object instance/tree from where to retrieve sub-objects
  27. * @param path a (RFC6901) string notation for the sub-object to retrieve
  28. * @param res a pointer where to store a reference to the json_object
  29. * associated with the given path
  30. *
  31. * @return negative if an error (or not found), or 0 if succeeded
  32. */
  33. int json_pointer_get(struct json_object *obj, const char *path, struct json_object **res);
  34. /**
  35. * Sets JSON object 'value' in the 'obj' tree at the location specified
  36. * by the 'path'. 'path' is JSON pointer notation as defined in RFC 6901
  37. * https://tools.ietf.org/html/rfc6901
  38. *
  39. * Note that 'obj' is a double pointer, mostly for the "" (empty string)
  40. * case, where the entire JSON object would be replaced by 'value'.
  41. * In the case of the "" path, the object at '*obj' will have it's refcount
  42. * decremented with 'json_object_put()' and the 'value' object will be assigned to it.
  43. *
  44. * For other cases (JSON sub-objects) ownership of 'value' will be transferred into
  45. * '*obj' via 'json_object_object_add()' & 'json_object_array_put_idx()', so the
  46. * only time the refcount should be decremented for 'value' is when the return value of
  47. * 'json_pointer_set()' is negative (meaning the 'value' object did not get set into '*obj').
  48. *
  49. * That also implies that 'json_pointer_set()' does not do any refcount incrementing.
  50. * (Just that single decrement that was mentioned above).
  51. *
  52. * @param obj the json_object instance/tree to which to add a sub-object
  53. * @param path a (RFC6901) string notation for the sub-object to set in the tree
  54. * @param value object to set at path
  55. *
  56. * @return negative if an error (or not found), or 0 if succeeded
  57. */
  58. int json_pointer_set(struct json_object **obj, const char *path, struct json_object *value);
  59. #ifdef __cplusplus
  60. }
  61. #endif
  62. #endif