Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:19:15

0001 /**
0002 *******************************************************************************
0003 * @file json_object_iterator.h
0004 *
0005 * Copyright (c) 2009-2012 Hewlett-Packard Development Company, L.P.
0006 *
0007 * This library is free software; you can redistribute it and/or modify
0008 * it under the terms of the MIT license. See COPYING for details.
0009 *
0010 * @brief  An API for iterating over json_type_object objects,
0011 *         styled to be familiar to C++ programmers.
0012 *         Unlike json_object_object_foreach() and
0013 *         json_object_object_foreachC(), this avoids the need to expose
0014 *         json-c internals like lh_entry.
0015 *
0016 * API attributes: <br>
0017 *   * Thread-safe: NO<br>
0018 *   * Reentrant: NO
0019 *
0020 *******************************************************************************
0021 */
0022 
0023 #ifndef JSON_OBJECT_ITERATOR_H
0024 #define JSON_OBJECT_ITERATOR_H
0025 
0026 #include "json_types.h"
0027 #include <stddef.h>
0028 
0029 #ifdef __cplusplus
0030 extern "C" {
0031 #endif
0032 
0033 /**
0034  * Forward declaration for the opaque iterator information.
0035  */
0036 struct json_object_iter_info_;
0037 
0038 /**
0039  * The opaque iterator that references a name/value pair within
0040  * a JSON Object instance or the "end" iterator value.
0041  */
0042 struct json_object_iterator
0043 {
0044     const void *opaque_;
0045 };
0046 
0047 /**
0048  * forward declaration of json-c's JSON value instance structure
0049  */
0050 struct json_object;
0051 
0052 /**
0053  * Initializes an iterator structure to a "default" value that
0054  * is convenient for initializing an iterator variable to a
0055  * default state (e.g., initialization list in a class'
0056  * constructor).
0057  *
0058  * @code
0059  * struct json_object_iterator iter = json_object_iter_init_default();
0060  * MyClass() : iter_(json_object_iter_init_default())
0061  * @endcode
0062  *
0063  * @note The initialized value doesn't reference any specific
0064  *       pair, is considered an invalid iterator, and MUST NOT
0065  *       be passed to any json-c API that expects a valid
0066  *       iterator.
0067  *
0068  * @note User and internal code MUST NOT make any assumptions
0069  *       about and dependencies on the value of the "default"
0070  *       iterator value.
0071  *
0072  * @return json_object_iterator
0073  */
0074 JSON_EXPORT struct json_object_iterator json_object_iter_init_default(void);
0075 
0076 /** Retrieves an iterator to the first pair of the JSON Object.
0077  *
0078  * @warning     Any modification of the underlying pair invalidates all
0079  *      iterators to that pair.
0080  *
0081  * @param obj   JSON Object instance (MUST be of type json_object)
0082  *
0083  * @return json_object_iterator If the JSON Object has at
0084  *              least one pair, on return, the iterator refers
0085  *              to the first pair. If the JSON Object doesn't
0086  *              have any pairs, the returned iterator is
0087  *              equivalent to the "end" iterator for the same
0088  *              JSON Object instance.
0089  *
0090  * @code
0091  * struct json_object_iterator it;
0092  * struct json_object_iterator itEnd;
0093  * struct json_object* obj;
0094  *
0095  * obj = json_tokener_parse("{'first':'george', 'age':100}");
0096  * it = json_object_iter_begin(obj);
0097  * itEnd = json_object_iter_end(obj);
0098  *
0099  * while (!json_object_iter_equal(&it, &itEnd)) {
0100  *     printf("%s\n",
0101  *            json_object_iter_peek_name(&it));
0102  *     json_object_iter_next(&it);
0103  * }
0104  *
0105  * @endcode
0106  */
0107 JSON_EXPORT struct json_object_iterator json_object_iter_begin(struct json_object *obj);
0108 
0109 /** Retrieves the iterator that represents the position beyond the
0110  *  last pair of the given JSON Object instance.
0111  *
0112  *  @warning Do NOT write code that assumes that the "end"
0113  *        iterator value is NULL, even if it is so in a
0114  *        particular instance of the implementation.
0115  *
0116  *  @note The reason we do not (and MUST NOT) provide
0117  *        "json_object_iter_is_end(json_object_iterator* iter)"
0118  *        type of API is because it would limit the underlying
0119  *        representation of name/value containment (or force us
0120  *        to add additional, otherwise unnecessary, fields to
0121  *        the iterator structure). The "end" iterator and the
0122  *        equality test method, on the other hand, permit us to
0123  *        cleanly abstract pretty much any reasonable underlying
0124  *        representation without burdening the iterator
0125  *        structure with unnecessary data.
0126  *
0127  *  @note For performance reasons, memorize the "end" iterator prior
0128  *        to any loop.
0129  *
0130  * @param obj JSON Object instance (MUST be of type json_object)
0131  *
0132  * @return json_object_iterator On return, the iterator refers
0133  *              to the "end" of the Object instance's pairs
0134  *              (i.e., NOT the last pair, but "beyond the last
0135  *              pair" value)
0136  */
0137 JSON_EXPORT struct json_object_iterator json_object_iter_end(const struct json_object *obj);
0138 
0139 /** Returns an iterator to the next pair, if any
0140  *
0141  * @warning Any modification of the underlying pair
0142  *          invalidates all iterators to that pair.
0143  *
0144  * @param iter [IN/OUT] Pointer to iterator that references a
0145  *         name/value pair; MUST be a valid, non-end iterator.
0146  *         WARNING: bad things will happen if invalid or "end"
0147  *         iterator is passed. Upon return will contain the
0148  *         reference to the next pair if there is one; if there
0149  *         are no more pairs, will contain the "end" iterator
0150  *         value, which may be compared against the return value
0151  *         of json_object_iter_end() for the same JSON Object
0152  *         instance.
0153  */
0154 JSON_EXPORT void json_object_iter_next(struct json_object_iterator *iter);
0155 
0156 /** Returns a const pointer to the name of the pair referenced
0157  *  by the given iterator.
0158  *
0159  * @param iter pointer to iterator that references a name/value
0160  *             pair; MUST be a valid, non-end iterator.
0161  *
0162  * @warning bad things will happen if an invalid or
0163  *              "end" iterator is passed.
0164  *
0165  * @return const char* Pointer to the name of the referenced
0166  *         name/value pair.  The name memory belongs to the
0167  *         name/value pair, will be freed when the pair is
0168  *         deleted or modified, and MUST NOT be modified or
0169  *         freed by the user.
0170  */
0171 JSON_EXPORT const char *json_object_iter_peek_name(const struct json_object_iterator *iter);
0172 
0173 /** Returns a pointer to the json-c instance representing the
0174  *  value of the referenced name/value pair, without altering
0175  *  the instance's reference count.
0176  *
0177  * @param iter  pointer to iterator that references a name/value
0178  *              pair; MUST be a valid, non-end iterator.
0179  *
0180  * @warning bad things will happen if invalid or
0181  *             "end" iterator is passed.
0182  *
0183  * @return struct json_object* Pointer to the json-c value
0184  *         instance of the referenced name/value pair;  the
0185  *         value's reference count is not changed by this
0186  *         function: if you plan to hold on to this json-c node,
0187  *         take a look at json_object_get() and
0188  *         json_object_put(). IMPORTANT: json-c API represents
0189  *         the JSON Null value as a NULL json_object instance
0190  *         pointer.
0191  */
0192 JSON_EXPORT struct json_object *
0193 json_object_iter_peek_value(const struct json_object_iterator *iter);
0194 
0195 /** Tests two iterators for equality.  Typically used to test
0196  *  for end of iteration by comparing an iterator to the
0197  *  corresponding "end" iterator (that was derived from the same
0198  *  JSON Object instance).
0199  *
0200  *  @note The reason we do not (and MUST NOT) provide
0201  *        "json_object_iter_is_end(json_object_iterator* iter)"
0202  *        type of API is because it would limit the underlying
0203  *        representation of name/value containment (or force us
0204  *        to add additional, otherwise unnecessary, fields to
0205  *        the iterator structure). The equality test method, on
0206  *        the other hand, permits us to cleanly abstract pretty
0207  *        much any reasonable underlying representation.
0208  *
0209  * @param iter1 Pointer to first valid, non-NULL iterator
0210  * @param iter2 POinter to second valid, non-NULL iterator
0211  *
0212  * @warning if a NULL iterator pointer or an uninitialized
0213  *          or invalid iterator, or iterators derived from
0214  *          different JSON Object instances are passed, bad things
0215  *          will happen!
0216  *
0217  * @return json_bool non-zero if iterators are equal (i.e., both
0218  *         reference the same name/value pair or are both at
0219  *         "end"); zero if they are not equal.
0220  */
0221 JSON_EXPORT json_bool json_object_iter_equal(const struct json_object_iterator *iter1,
0222                                              const struct json_object_iterator *iter2);
0223 
0224 #ifdef __cplusplus
0225 }
0226 #endif
0227 
0228 #endif /* JSON_OBJECT_ITERATOR_H */