blob: 3221a15708d21af8d9198851e731329482489d9b [file] [log] [blame]
Keith Derrickded667a2012-04-23 15:34:44 -07001/**
Keith Derrickbcfd1f52012-04-12 09:54:21 -07002*******************************************************************************
3* @file json_object_iterator.h
Keith Derrickded667a2012-04-23 15:34:44 -07004*
5* Copyright (c) 2009-2012 Hewlett-Packard Development Company, L.P.
Keith Derrickbcfd1f52012-04-12 09:54:21 -07006*
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.
Keith Derrickded667a2012-04-23 15:34:44 -07009*
10* @brief json-c forces clients to use its private data
Keith Derrickbcfd1f52012-04-12 09:54:21 -070011* structures for JSON Object iteration. This API
Keith Derrickded667a2012-04-23 15:34:44 -070012* corrects that by abstracting the private json-c
Keith Derrickbcfd1f52012-04-12 09:54:21 -070013* details.
Keith Derrickded667a2012-04-23 15:34:44 -070014*
15* API attributes: <br>
16* * Thread-safe: NO<br>
17* * Reentrant: NO
18*
Keith Derrickbcfd1f52012-04-12 09:54:21 -070019*******************************************************************************
20*/
21
22
23#ifndef JSON_OBJECT_ITERATOR_H
24#define JSON_OBJECT_ITERATOR_H
25
26#include <stddef.h>
27#include <stdbool.h>
28
Keith Derrickded667a2012-04-23 15:34:44 -070029#ifdef __cplusplus
Keith Derrickbcfd1f52012-04-12 09:54:21 -070030extern "C" {
31#endif
32
33/**
34 * Forward declaration for the opaque iterator information.
35 */
36struct json_object_iter_info_;
37
38/**
39 * The opaque iterator that references a name/value pair within
Keith Derrickded667a2012-04-23 15:34:44 -070040 * a JSON Object instance or the "end" iterator value.
Keith Derrickbcfd1f52012-04-12 09:54:21 -070041 */
42struct json_object_iterator {
43 const void* opaque_;
44};
45
46
47/**
Keith Derrickded667a2012-04-23 15:34:44 -070048 * forward declaration of json-c's JSON value instance structure
Keith Derrickbcfd1f52012-04-12 09:54:21 -070049 */
50struct json_object;
51
52
53/**
54 * Initializes an iterator structure to a "default" value that
55 * is convenient for initializing an iterator variable to a
56 * default state (e.g., initialization list in a class'
57 * constructor).
Keith Derrickded667a2012-04-23 15:34:44 -070058 *
59 * @code
Keith Derrickbcfd1f52012-04-12 09:54:21 -070060 * struct json_object_iterator iter = json_object_iter_init_default();
61 * MyClass() : iter_(json_object_iter_init_default())
62 * @endcode
Keith Derrickded667a2012-04-23 15:34:44 -070063 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -070064 * @note The initialized value doesn't reference any specific
65 * pair, is considered an invalid iterator, and MUST NOT
Keith Derrickded667a2012-04-23 15:34:44 -070066 * be passed to any json-c API that expects a valid
Keith Derrickbcfd1f52012-04-12 09:54:21 -070067 * iterator.
Keith Derrickded667a2012-04-23 15:34:44 -070068 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -070069 * @note User and internal code MUST NOT make any assumptions
70 * about and dependencies on the value of the "default"
71 * iterator value.
Keith Derrickded667a2012-04-23 15:34:44 -070072 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -070073 * @return json_object_iterator
74 */
75struct json_object_iterator
76json_object_iter_init_default(void);
77
78/** Retrieves an iterator to the first pair of the JSON Object.
Keith Derrickded667a2012-04-23 15:34:44 -070079 *
80 * @warning Any modification of the underlying pair invalidates all
81 * iterators to that pair.
82 *
83 * @param obj JSON Object instance (MUST be of type json_object)
Keith Derrickbcfd1f52012-04-12 09:54:21 -070084 *
85 * @return json_object_iterator If the JSON Object has at
86 * least one pair, on return, the iterator refers
87 * to the first pair. If the JSON Object doesn't
88 * have any pairs, the returned iterator is
89 * equivalent to the "end" iterator for the same
90 * JSON Object instance.
Keith Derrickded667a2012-04-23 15:34:44 -070091 *
92 * @code
Keith Derrickbcfd1f52012-04-12 09:54:21 -070093 * struct json_object_iterator it;
94 * struct json_object_iterator itEnd;
Keith Derrickded667a2012-04-23 15:34:44 -070095 * struct json_object* obj;
96 *
97 * obj = json_tokener_parse("{'first':'george', 'age':100}");
98 * it = json_object_iter_begin(obj);
99 * itEnd = json_object_iter_end(obj);
100 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700101 * while (!json_object_iter_equal(&it, &itEnd)) {
102 * printf("%s\n",
103 * json_object_iter_peek_name(&it));
104 * json_object_iter_next(&it);
105 * }
106 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700107 * @endcode
108 */
109struct json_object_iterator
110json_object_iter_begin(struct json_object* obj);
111
112/** Retrieves the iterator that represents the position beyond the
113 * last pair of the given JSON Object instance.
Keith Derrickded667a2012-04-23 15:34:44 -0700114 *
115 * @warning Do NOT write code that assumes that the "end"
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700116 * iterator value is NULL, even if it is so in a
117 * particular instance of the implementation.
Keith Derrickded667a2012-04-23 15:34:44 -0700118 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700119 * @note The reason we do not (and MUST NOT) provide
120 * "json_object_iter_is_end(json_object_iterator* iter)"
121 * type of API is because it would limit the underlying
122 * representation of name/value containment (or force us
123 * to add additional, otherwise unnecessary, fields to
124 * the iterator structure). The "end" iterator and the
125 * equality test method, on the other hand, permit us to
126 * cleanly abstract pretty much any reasonable underlying
127 * representation without burdening the iterator
128 * structure with unnecessary data.
129 *
Keith Derrickded667a2012-04-23 15:34:44 -0700130 * @note For performance reasons, memorize the "end" iterator prior
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700131 * to any loop.
Keith Derrickded667a2012-04-23 15:34:44 -0700132 *
133 * @param obj JSON Object instance (MUST be of type json_object)
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700134 *
135 * @return json_object_iterator On return, the iterator refers
136 * to the "end" of the Object instance's pairs
137 * (i.e., NOT the last pair, but "beyond the last
138 * pair" value)
139 */
140struct json_object_iterator
141json_object_iter_end(const struct json_object* obj);
142
143/** Returns an iterator to the next pair, if any
Keith Derrickded667a2012-04-23 15:34:44 -0700144 *
145 * @warning Any modification of the underlying pair
146 * invalidates all iterators to that pair.
147 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700148 * @param iter [IN/OUT] Pointer to iterator that references a
149 * name/value pair; MUST be a valid, non-end iterator.
150 * WARNING: bad things will happen if invalid or "end"
151 * iterator is passed. Upon return will contain the
152 * reference to the next pair if there is one; if there
153 * are no more pairs, will contain the "end" iterator
154 * value, which may be compared against the return value
155 * of json_object_iter_end() for the same JSON Object
156 * instance.
157 */
158void
159json_object_iter_next(struct json_object_iterator* iter);
160
161
162/** Returns a const pointer to the name of the pair referenced
163 * by the given iterator.
Keith Derrickded667a2012-04-23 15:34:44 -0700164 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700165 * @param iter pointer to iterator that references a name/value
166 * pair; MUST be a valid, non-end iterator.
Keith Derrickded667a2012-04-23 15:34:44 -0700167 *
168 * @warning bad things will happen if an invalid or
169 * "end" iterator is passed.
170 *
171 * @return const char* Pointer to the name of the referenced
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700172 * 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 */
177const char*
178json_object_iter_peek_name(const struct json_object_iterator* iter);
179
180
Keith Derrickded667a2012-04-23 15:34:44 -0700181/** Returns a pointer to the json-c instance representing the
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700182 * value of the referenced name/value pair, without altering
183 * the instance's reference count.
Keith Derrickded667a2012-04-23 15:34:44 -0700184 *
185 * @param iter pointer to iterator that references a name/value
186 * pair; MUST be a valid, non-end iterator.
187 *
188 * @warning bad things will happen if invalid or
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700189 * "end" iterator is passed.
Keith Derrickded667a2012-04-23 15:34:44 -0700190 *
191 * @return struct json_object* Pointer to the json-c value
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700192 * instance of the referenced name/value pair; the
193 * value's reference count is not changed by this
Keith Derrickded667a2012-04-23 15:34:44 -0700194 * function: if you plan to hold on to this json-c node,
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700195 * take a look at json_object_get() and
Keith Derrickded667a2012-04-23 15:34:44 -0700196 * json_object_put(). IMPORTANT: json-c API represents
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700197 * the JSON Null value as a NULL json_object instance
198 * pointer.
199 */
200struct json_object*
201json_object_iter_peek_value(const struct json_object_iterator* iter);
202
203
204/** Tests two iterators for equality. Typically used to test
205 * for end of iteration by comparing an iterator to the
206 * corresponding "end" iterator (that was derived from the same
207 * JSON Object instance).
Keith Derrickded667a2012-04-23 15:34:44 -0700208 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700209 * @note The reason we do not (and MUST NOT) provide
210 * "json_object_iter_is_end(json_object_iterator* iter)"
211 * type of API is because it would limit the underlying
212 * representation of name/value containment (or force us
213 * to add additional, otherwise unnecessary, fields to
214 * the iterator structure). The equality test method, on
215 * the other hand, permits us to cleanly abstract pretty
216 * much any reasonable underlying representation.
Keith Derrickded667a2012-04-23 15:34:44 -0700217 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700218 * @param iter1 Pointer to first valid, non-NULL iterator
219 * @param iter2 POinter to second valid, non-NULL iterator
Keith Derrickded667a2012-04-23 15:34:44 -0700220 *
221 * @warning if a NULL iterator pointer or an uninitialized
222 * or invalid iterator, or iterators derived from
223 * different JSON Object instances are passed, bad things
224 * will happen!
225 *
Keith Derrickbcfd1f52012-04-12 09:54:21 -0700226 * @return bool non-zero if iterators are equal (i.e., both
227 * reference the same name/value pair or are both at
228 * "end"); zero if they are not equal.
229 */
230bool
231json_object_iter_equal(const struct json_object_iterator* iter1,
232 const struct json_object_iterator* iter2);
233
234
235#ifdef __cplusplus
236}
237#endif
238
239
240#endif // JSON_OBJECT_ITERATOR_H