2 *******************************************************************************
3 * @file json_object_iterator.h
5 * Copyright (c) 2009-2012 Hewlett-Packard Development Company, L.P.
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.
10 * @brief json-c forces clients to use its private data
11 * structures for JSON Object iteration. This API
12 * corrects that by abstracting the private json-c
15 * API attributes: <br>
16 * * Thread-safe: NO<br>
19 *******************************************************************************
23 #ifndef JSON_OBJECT_ITERATOR_H
24 #define JSON_OBJECT_ITERATOR_H
33 * Forward declaration for the opaque iterator information.
35 struct json_object_iter_info_;
38 * The opaque iterator that references a name/value pair within
39 * a JSON Object instance or the "end" iterator value.
41 struct json_object_iterator {
47 * forward declaration of json-c's JSON value instance structure
53 * Initializes an iterator structure to a "default" value that
54 * is convenient for initializing an iterator variable to a
55 * default state (e.g., initialization list in a class'
59 * struct json_object_iterator iter = json_object_iter_init_default();
60 * MyClass() : iter_(json_object_iter_init_default())
63 * @note The initialized value doesn't reference any specific
64 * pair, is considered an invalid iterator, and MUST NOT
65 * be passed to any json-c API that expects a valid
68 * @note User and internal code MUST NOT make any assumptions
69 * about and dependencies on the value of the "default"
72 * @return json_object_iterator
74 struct json_object_iterator
75 json_object_iter_init_default(void);
77 /** Retrieves an iterator to the first pair of the JSON Object.
79 * @warning Any modification of the underlying pair invalidates all
80 * iterators to that pair.
82 * @param obj JSON Object instance (MUST be of type json_object)
84 * @return json_object_iterator If the JSON Object has at
85 * least one pair, on return, the iterator refers
86 * to the first pair. If the JSON Object doesn't
87 * have any pairs, the returned iterator is
88 * equivalent to the "end" iterator for the same
89 * JSON Object instance.
92 * struct json_object_iterator it;
93 * struct json_object_iterator itEnd;
94 * struct json_object* obj;
96 * obj = json_tokener_parse("{'first':'george', 'age':100}");
97 * it = json_object_iter_begin(obj);
98 * itEnd = json_object_iter_end(obj);
100 * while (!json_object_iter_equal(&it, &itEnd)) {
102 * json_object_iter_peek_name(&it));
103 * json_object_iter_next(&it);
108 struct json_object_iterator
109 json_object_iter_begin(struct json_object* obj);
111 /** Retrieves the iterator that represents the position beyond the
112 * last pair of the given JSON Object instance.
114 * @warning Do NOT write code that assumes that the "end"
115 * iterator value is NULL, even if it is so in a
116 * particular instance of the implementation.
118 * @note The reason we do not (and MUST NOT) provide
119 * "json_object_iter_is_end(json_object_iterator* iter)"
120 * type of API is because it would limit the underlying
121 * representation of name/value containment (or force us
122 * to add additional, otherwise unnecessary, fields to
123 * the iterator structure). The "end" iterator and the
124 * equality test method, on the other hand, permit us to
125 * cleanly abstract pretty much any reasonable underlying
126 * representation without burdening the iterator
127 * structure with unnecessary data.
129 * @note For performance reasons, memorize the "end" iterator prior
132 * @param obj JSON Object instance (MUST be of type json_object)
134 * @return json_object_iterator On return, the iterator refers
135 * to the "end" of the Object instance's pairs
136 * (i.e., NOT the last pair, but "beyond the last
139 struct json_object_iterator
140 json_object_iter_end(const struct json_object* obj);
142 /** Returns an iterator to the next pair, if any
144 * @warning Any modification of the underlying pair
145 * invalidates all iterators to that pair.
147 * @param iter [IN/OUT] Pointer to iterator that references a
148 * name/value pair; MUST be a valid, non-end iterator.
149 * WARNING: bad things will happen if invalid or "end"
150 * iterator is passed. Upon return will contain the
151 * reference to the next pair if there is one; if there
152 * are no more pairs, will contain the "end" iterator
153 * value, which may be compared against the return value
154 * of json_object_iter_end() for the same JSON Object
158 json_object_iter_next(struct json_object_iterator* iter);
161 /** Returns a const pointer to the name of the pair referenced
162 * by the given iterator.
164 * @param iter pointer to iterator that references a name/value
165 * pair; MUST be a valid, non-end iterator.
167 * @warning bad things will happen if an invalid or
168 * "end" iterator is passed.
170 * @return const char* Pointer to the name of the referenced
171 * name/value pair. The name memory belongs to the
172 * name/value pair, will be freed when the pair is
173 * deleted or modified, and MUST NOT be modified or
177 json_object_iter_peek_name(const struct json_object_iterator* iter);
180 /** Returns a pointer to the json-c instance representing the
181 * value of the referenced name/value pair, without altering
182 * the instance's reference count.
184 * @param iter pointer to iterator that references a name/value
185 * pair; MUST be a valid, non-end iterator.
187 * @warning bad things will happen if invalid or
188 * "end" iterator is passed.
190 * @return struct json_object* Pointer to the json-c value
191 * instance of the referenced name/value pair; the
192 * value's reference count is not changed by this
193 * function: if you plan to hold on to this json-c node,
194 * take a look at json_object_get() and
195 * json_object_put(). IMPORTANT: json-c API represents
196 * the JSON Null value as a NULL json_object instance
200 json_object_iter_peek_value(const struct json_object_iterator* iter);
203 /** Tests two iterators for equality. Typically used to test
204 * for end of iteration by comparing an iterator to the
205 * corresponding "end" iterator (that was derived from the same
206 * JSON Object instance).
208 * @note The reason we do not (and MUST NOT) provide
209 * "json_object_iter_is_end(json_object_iterator* iter)"
210 * type of API is because it would limit the underlying
211 * representation of name/value containment (or force us
212 * to add additional, otherwise unnecessary, fields to
213 * the iterator structure). The equality test method, on
214 * the other hand, permits us to cleanly abstract pretty
215 * much any reasonable underlying representation.
217 * @param iter1 Pointer to first valid, non-NULL iterator
218 * @param iter2 POinter to second valid, non-NULL iterator
220 * @warning if a NULL iterator pointer or an uninitialized
221 * or invalid iterator, or iterators derived from
222 * different JSON Object instances are passed, bad things
225 * @return json_bool non-zero if iterators are equal (i.e., both
226 * reference the same name/value pair or are both at
227 * "end"); zero if they are not equal.
230 json_object_iter_equal(const struct json_object_iterator* iter1,
231 const struct json_object_iterator* iter2);
239 #endif // JSON_OBJECT_ITERATOR_H