json_object_iterator.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. *******************************************************************************
  3. * @file json_object_iterator.h
  4. *
  5. * Copyright (c) 2009-2012 Hewlett-Packard Development Company, L.P.
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. * @brief An API for iterating over json_type_object objects,
  11. * styled to be familiar to C++ programmers.
  12. * Unlike json_object_object_foreach() and
  13. * json_object_object_foreachC(), this avoids the need to expose
  14. * json-c internals like lh_entry.
  15. *
  16. * API attributes: <br>
  17. * * Thread-safe: NO<br>
  18. * * Reentrant: NO
  19. *
  20. *******************************************************************************
  21. */
  22. #ifndef JSON_OBJECT_ITERATOR_H
  23. #define JSON_OBJECT_ITERATOR_H
  24. #include <stddef.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * Forward declaration for the opaque iterator information.
  30. */
  31. struct json_object_iter_info_;
  32. /**
  33. * The opaque iterator that references a name/value pair within
  34. * a JSON Object instance or the "end" iterator value.
  35. */
  36. struct json_object_iterator {
  37. const void* opaque_;
  38. };
  39. /**
  40. * forward declaration of json-c's JSON value instance structure
  41. */
  42. struct json_object;
  43. /**
  44. * Initializes an iterator structure to a "default" value that
  45. * is convenient for initializing an iterator variable to a
  46. * default state (e.g., initialization list in a class'
  47. * constructor).
  48. *
  49. * @code
  50. * struct json_object_iterator iter = json_object_iter_init_default();
  51. * MyClass() : iter_(json_object_iter_init_default())
  52. * @endcode
  53. *
  54. * @note The initialized value doesn't reference any specific
  55. * pair, is considered an invalid iterator, and MUST NOT
  56. * be passed to any json-c API that expects a valid
  57. * iterator.
  58. *
  59. * @note User and internal code MUST NOT make any assumptions
  60. * about and dependencies on the value of the "default"
  61. * iterator value.
  62. *
  63. * @return json_object_iterator
  64. */
  65. struct json_object_iterator
  66. json_object_iter_init_default(void);
  67. /** Retrieves an iterator to the first pair of the JSON Object.
  68. *
  69. * @warning Any modification of the underlying pair invalidates all
  70. * iterators to that pair.
  71. *
  72. * @param obj JSON Object instance (MUST be of type json_object)
  73. *
  74. * @return json_object_iterator If the JSON Object has at
  75. * least one pair, on return, the iterator refers
  76. * to the first pair. If the JSON Object doesn't
  77. * have any pairs, the returned iterator is
  78. * equivalent to the "end" iterator for the same
  79. * JSON Object instance.
  80. *
  81. * @code
  82. * struct json_object_iterator it;
  83. * struct json_object_iterator itEnd;
  84. * struct json_object* obj;
  85. *
  86. * obj = json_tokener_parse("{'first':'george', 'age':100}");
  87. * it = json_object_iter_begin(obj);
  88. * itEnd = json_object_iter_end(obj);
  89. *
  90. * while (!json_object_iter_equal(&it, &itEnd)) {
  91. * printf("%s\n",
  92. * json_object_iter_peek_name(&it));
  93. * json_object_iter_next(&it);
  94. * }
  95. *
  96. * @endcode
  97. */
  98. struct json_object_iterator
  99. json_object_iter_begin(struct json_object* obj);
  100. /** Retrieves the iterator that represents the position beyond the
  101. * last pair of the given JSON Object instance.
  102. *
  103. * @warning Do NOT write code that assumes that the "end"
  104. * iterator value is NULL, even if it is so in a
  105. * particular instance of the implementation.
  106. *
  107. * @note The reason we do not (and MUST NOT) provide
  108. * "json_object_iter_is_end(json_object_iterator* iter)"
  109. * type of API is because it would limit the underlying
  110. * representation of name/value containment (or force us
  111. * to add additional, otherwise unnecessary, fields to
  112. * the iterator structure). The "end" iterator and the
  113. * equality test method, on the other hand, permit us to
  114. * cleanly abstract pretty much any reasonable underlying
  115. * representation without burdening the iterator
  116. * structure with unnecessary data.
  117. *
  118. * @note For performance reasons, memorize the "end" iterator prior
  119. * to any loop.
  120. *
  121. * @param obj JSON Object instance (MUST be of type json_object)
  122. *
  123. * @return json_object_iterator On return, the iterator refers
  124. * to the "end" of the Object instance's pairs
  125. * (i.e., NOT the last pair, but "beyond the last
  126. * pair" value)
  127. */
  128. struct json_object_iterator
  129. json_object_iter_end(const struct json_object* obj);
  130. /** Returns an iterator to the next pair, if any
  131. *
  132. * @warning Any modification of the underlying pair
  133. * invalidates all iterators to that pair.
  134. *
  135. * @param iter [IN/OUT] Pointer to iterator that references a
  136. * name/value pair; MUST be a valid, non-end iterator.
  137. * WARNING: bad things will happen if invalid or "end"
  138. * iterator is passed. Upon return will contain the
  139. * reference to the next pair if there is one; if there
  140. * are no more pairs, will contain the "end" iterator
  141. * value, which may be compared against the return value
  142. * of json_object_iter_end() for the same JSON Object
  143. * instance.
  144. */
  145. void
  146. json_object_iter_next(struct json_object_iterator* iter);
  147. /** Returns a const pointer to the name of the pair referenced
  148. * by the given iterator.
  149. *
  150. * @param iter pointer to iterator that references a name/value
  151. * pair; MUST be a valid, non-end iterator.
  152. *
  153. * @warning bad things will happen if an invalid or
  154. * "end" iterator is passed.
  155. *
  156. * @return const char* Pointer to the name of the referenced
  157. * name/value pair. The name memory belongs to the
  158. * name/value pair, will be freed when the pair is
  159. * deleted or modified, and MUST NOT be modified or
  160. * freed by the user.
  161. */
  162. const char*
  163. json_object_iter_peek_name(const struct json_object_iterator* iter);
  164. /** Returns a pointer to the json-c instance representing the
  165. * value of the referenced name/value pair, without altering
  166. * the instance's reference count.
  167. *
  168. * @param iter pointer to iterator that references a name/value
  169. * pair; MUST be a valid, non-end iterator.
  170. *
  171. * @warning bad things will happen if invalid or
  172. * "end" iterator is passed.
  173. *
  174. * @return struct json_object* Pointer to the json-c value
  175. * instance of the referenced name/value pair; the
  176. * value's reference count is not changed by this
  177. * function: if you plan to hold on to this json-c node,
  178. * take a look at json_object_get() and
  179. * json_object_put(). IMPORTANT: json-c API represents
  180. * the JSON Null value as a NULL json_object instance
  181. * pointer.
  182. */
  183. struct json_object*
  184. json_object_iter_peek_value(const struct json_object_iterator* iter);
  185. /** Tests two iterators for equality. Typically used to test
  186. * for end of iteration by comparing an iterator to the
  187. * corresponding "end" iterator (that was derived from the same
  188. * JSON Object instance).
  189. *
  190. * @note The reason we do not (and MUST NOT) provide
  191. * "json_object_iter_is_end(json_object_iterator* iter)"
  192. * type of API is because it would limit the underlying
  193. * representation of name/value containment (or force us
  194. * to add additional, otherwise unnecessary, fields to
  195. * the iterator structure). The equality test method, on
  196. * the other hand, permits us to cleanly abstract pretty
  197. * much any reasonable underlying representation.
  198. *
  199. * @param iter1 Pointer to first valid, non-NULL iterator
  200. * @param iter2 POinter to second valid, non-NULL iterator
  201. *
  202. * @warning if a NULL iterator pointer or an uninitialized
  203. * or invalid iterator, or iterators derived from
  204. * different JSON Object instances are passed, bad things
  205. * will happen!
  206. *
  207. * @return json_bool non-zero if iterators are equal (i.e., both
  208. * reference the same name/value pair or are both at
  209. * "end"); zero if they are not equal.
  210. */
  211. json_bool
  212. json_object_iter_equal(const struct json_object_iterator* iter1,
  213. const struct json_object_iterator* iter2);
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #endif /* JSON_OBJECT_ITERATOR_H */