json_visit.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef _json_c_json_visit_h_
  2. #define _json_c_json_visit_h_
  3. /**
  4. * @file
  5. * @brief Methods for walking a tree of objects.
  6. */
  7. #include "json_object.h"
  8. typedef int (json_c_visit_userfunc)(json_object *jso, int flags,
  9. json_object *parent_jso, const char *jso_key,
  10. size_t *jso_index, void *userarg);
  11. /**
  12. * Visit each object in the JSON hierarchy starting at jso.
  13. * For each object, userfunc is called, passing the object and userarg.
  14. * If the object has a parent (i.e. anything other than jso itself)
  15. * its parent will be passed as parent_jso, and either jso_key or jso_index
  16. * will be set, depending on whether the parent is an object or an array.
  17. *
  18. * Nodes will be visited depth first, but containers (arrays and objects)
  19. * will be visited twice, the second time with JSON_C_VISIT_SECOND set in
  20. * flags.
  21. *
  22. * userfunc must return one of the defined return values, to indicate
  23. * whether and how to continue visiting nodes, or one of various ways to stop.
  24. *
  25. * Returns 0 if nodes were visited successfully, even if some were
  26. * intentionally skipped due to what userfunc returned.
  27. * Returns <0 if an error occurred during iteration, including if
  28. * userfunc returned JSON_C_VISIT_RETURN_ERROR.
  29. */
  30. int json_c_visit(json_object *jso, int future_flags,
  31. json_c_visit_userfunc *userfunc, void *userarg);
  32. /**
  33. * Passed to json_c_visit_userfunc as one of the flags values to indicate
  34. * that this is the second time a container (array or object) is being
  35. * called, after all of it's members have been iterated over.
  36. */
  37. #define JSON_C_VISIT_SECOND 0x02
  38. /**
  39. * This json_c_visit_userfunc return value indicates that iteration
  40. * should proceed normally.
  41. */
  42. #define JSON_C_VISIT_RETURN_CONTINUE 0
  43. /**
  44. * This json_c_visit_userfunc return value indicates that iteration
  45. * over the members of the current object should be skipped.
  46. * If the current object isn't a container (array or object), this
  47. * is no different than JSON_C_VISIT_RETURN_CONTINUE.
  48. */
  49. #define JSON_C_VISIT_RETURN_SKIP 7547
  50. /**
  51. * This json_c_visit_userfunc return value indicates that iteration
  52. * of the fields/elements of the <b>containing</b> object should stop
  53. * and continue "popped up" a level of the object hierarchy.
  54. * For example, returning this when handling arg will result in
  55. * arg3 and any other fields being skipped. The next call to userfunc
  56. * will be the JSON_C_VISIT_SECOND call on "foo", followed by a userfunc
  57. * call on "bar".
  58. * <pre>
  59. * {
  60. * "foo": {
  61. * "arg1": 1,
  62. * "arg2": 2,
  63. * "arg3": 3,
  64. * ...
  65. * },
  66. * "bar": {
  67. * ...
  68. * }
  69. * }
  70. * </pre>
  71. */
  72. #define JSON_C_VISIT_RETURN_POP 767
  73. /**
  74. * This json_c_visit_userfunc return value indicates that iteration
  75. * should stop immediately, and cause json_c_visit to return success.
  76. */
  77. #define JSON_C_VISIT_RETURN_STOP 7867
  78. /**
  79. * This json_c_visit_userfunc return value indicates that iteration
  80. * should stop immediately, and cause json_c_visit to return an error.
  81. */
  82. #define JSON_C_VISIT_RETURN_ERROR -1
  83. #endif /* _json_c_json_visit_h_ */