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_object_iterator.h 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "json_types.h"
  25. #include <stddef.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. * Forward declaration for the opaque iterator information.
  31. */
  32. struct json_object_iter_info_;
  33. /**
  34. * The opaque iterator that references a name/value pair within
  35. * a JSON Object instance or the "end" iterator value.
  36. */
  37. struct json_object_iterator
  38. {
  39. const void *opaque_;
  40. };
  41. /**
  42. * forward declaration of json-c's JSON value instance structure
  43. */
  44. struct json_object;
  45. /**
  46. * Initializes an iterator structure to a "default" value that
  47. * is convenient for initializing an iterator variable to a
  48. * default state (e.g., initialization list in a class'
  49. * constructor).
  50. *
  51. * @code
  52. * struct json_object_iterator iter = json_object_iter_init_default();
  53. * MyClass() : iter_(json_object_iter_init_default())
  54. * @endcode
  55. *
  56. * @note The initialized value doesn't reference any specific
  57. * pair, is considered an invalid iterator, and MUST NOT
  58. * be passed to any json-c API that expects a valid
  59. * iterator.
  60. *
  61. * @note User and internal code MUST NOT make any assumptions
  62. * about and dependencies on the value of the "default"
  63. * iterator value.
  64. *
  65. * @return json_object_iterator
  66. */
  67. JSON_EXPORT struct json_object_iterator json_object_iter_init_default(void);
  68. /** Retrieves an iterator to the first pair of the JSON Object.
  69. *
  70. * @warning Any modification of the underlying pair invalidates all
  71. * iterators to that pair.
  72. *
  73. * @param obj JSON Object instance (MUST be of type json_object)
  74. *
  75. * @return json_object_iterator If the JSON Object has at
  76. * least one pair, on return, the iterator refers
  77. * to the first pair. If the JSON Object doesn't
  78. * have any pairs, the returned iterator is
  79. * equivalent to the "end" iterator for the same
  80. * JSON Object instance.
  81. *
  82. * @code
  83. * struct json_object_iterator it;
  84. * struct json_object_iterator itEnd;
  85. * struct json_object* obj;
  86. *
  87. * obj = json_tokener_parse("{'first':'george', 'age':100}");
  88. * it = json_object_iter_begin(obj);
  89. * itEnd = json_object_iter_end(obj);
  90. *
  91. * while (!json_object_iter_equal(&it, &itEnd)) {
  92. * printf("%s\n",
  93. * json_object_iter_peek_name(&it));
  94. * json_object_iter_next(&it);
  95. * }
  96. *
  97. * @endcode
  98. */
  99. JSON_EXPORT struct json_object_iterator 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. JSON_EXPORT struct json_object_iterator json_object_iter_end(const struct json_object *obj);
  129. /** Returns an iterator to the next pair, if any
  130. *
  131. * @warning Any modification of the underlying pair
  132. * invalidates all iterators to that pair.
  133. *
  134. * @param iter [IN/OUT] Pointer to iterator that references a
  135. * name/value pair; MUST be a valid, non-end iterator.
  136. * WARNING: bad things will happen if invalid or "end"
  137. * iterator is passed. Upon return will contain the
  138. * reference to the next pair if there is one; if there
  139. * are no more pairs, will contain the "end" iterator
  140. * value, which may be compared against the return value
  141. * of json_object_iter_end() for the same JSON Object
  142. * instance.
  143. */
  144. JSON_EXPORT void json_object_iter_next(struct json_object_iterator *iter);
  145. /** Returns a const pointer to the name of the pair referenced
  146. * by the given iterator.
  147. *
  148. * @param iter pointer to iterator that references a name/value
  149. * pair; MUST be a valid, non-end iterator.
  150. *
  151. * @warning bad things will happen if an invalid or
  152. * "end" iterator is passed.
  153. *
  154. * @return const char* Pointer to the name of the referenced
  155. * name/value pair. The name memory belongs to the
  156. * name/value pair, will be freed when the pair is
  157. * deleted or modified, and MUST NOT be modified or
  158. * freed by the user.
  159. */
  160. JSON_EXPORT const struct json_key *
  161. json_object_iter_peek_name(const struct json_object_iterator *iter);
  162. /** Returns a pointer to the json-c instance representing the
  163. * value of the referenced name/value pair, without altering
  164. * the instance's reference count.
  165. *
  166. * @param iter pointer to iterator that references a name/value
  167. * pair; MUST be a valid, non-end iterator.
  168. *
  169. * @warning bad things will happen if invalid or
  170. * "end" iterator is passed.
  171. *
  172. * @return struct json_object* Pointer to the json-c value
  173. * instance of the referenced name/value pair; the
  174. * value's reference count is not changed by this
  175. * function: if you plan to hold on to this json-c node,
  176. * take a look at json_object_get() and
  177. * json_object_put(). IMPORTANT: json-c API represents
  178. * the JSON Null value as a NULL json_object instance
  179. * pointer.
  180. */
  181. JSON_EXPORT struct json_object *
  182. json_object_iter_peek_value(const struct json_object_iterator *iter);
  183. /** Tests two iterators for equality. Typically used to test
  184. * for end of iteration by comparing an iterator to the
  185. * corresponding "end" iterator (that was derived from the same
  186. * JSON Object instance).
  187. *
  188. * @note The reason we do not (and MUST NOT) provide
  189. * "json_object_iter_is_end(json_object_iterator* iter)"
  190. * type of API is because it would limit the underlying
  191. * representation of name/value containment (or force us
  192. * to add additional, otherwise unnecessary, fields to
  193. * the iterator structure). The equality test method, on
  194. * the other hand, permits us to cleanly abstract pretty
  195. * much any reasonable underlying representation.
  196. *
  197. * @param iter1 Pointer to first valid, non-NULL iterator
  198. * @param iter2 POinter to second valid, non-NULL iterator
  199. *
  200. * @warning if a NULL iterator pointer or an uninitialized
  201. * or invalid iterator, or iterators derived from
  202. * different JSON Object instances are passed, bad things
  203. * will happen!
  204. *
  205. * @return json_bool non-zero if iterators are equal (i.e., both
  206. * reference the same name/value pair or are both at
  207. * "end"); zero if they are not equal.
  208. */
  209. JSON_EXPORT json_bool json_object_iter_equal(const struct json_object_iterator *iter1,
  210. const struct json_object_iterator *iter2);
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #endif /* JSON_OBJECT_ITERATOR_H */