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

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