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

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