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

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