Blame json_object_iterator.h

Packit ea8578
/**
Packit ea8578
*******************************************************************************
Packit ea8578
* @file json_object_iterator.h
Packit ea8578
*
Packit ea8578
* Copyright (c) 2009-2012 Hewlett-Packard Development Company, L.P.
Packit ea8578
*
Packit ea8578
* This library is free software; you can redistribute it and/or modify
Packit ea8578
* it under the terms of the MIT license. See COPYING for details.
Packit ea8578
*
Packit ea8578
* @brief  An API for iterating over json_type_object objects,
Packit ea8578
*         styled to be familiar to C++ programmers.
Packit ea8578
*         Unlike json_object_object_foreach() and
Packit ea8578
*         json_object_object_foreachC(), this avoids the need to expose
Packit ea8578
*         json-c internals like lh_entry.
Packit ea8578
*
Packit ea8578
* API attributes: 
Packit ea8578
*   * Thread-safe: NO
Packit ea8578
*   * Reentrant: NO
Packit ea8578
*
Packit ea8578
*******************************************************************************
Packit ea8578
*/
Packit ea8578
Packit ea8578
Packit ea8578
#ifndef JSON_OBJECT_ITERATOR_H
Packit ea8578
#define JSON_OBJECT_ITERATOR_H
Packit ea8578
Packit ea8578
#include <stddef.h>
Packit ea8578
Packit ea8578
#ifdef __cplusplus
Packit ea8578
extern "C" {
Packit ea8578
#endif
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * Forward declaration for the opaque iterator information.
Packit ea8578
 */
Packit ea8578
struct json_object_iter_info_;
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * The opaque iterator that references a name/value pair within
Packit ea8578
 * a JSON Object instance or the "end" iterator value.
Packit ea8578
 */
Packit ea8578
struct json_object_iterator {
Packit ea8578
    const void* opaque_;
Packit ea8578
};
Packit ea8578
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * forward declaration of json-c's JSON value instance structure
Packit ea8578
 */
Packit ea8578
struct json_object;
Packit ea8578
Packit ea8578
Packit ea8578
/**
Packit ea8578
 * Initializes an iterator structure to a "default" value that
Packit ea8578
 * is convenient for initializing an iterator variable to a
Packit ea8578
 * default state (e.g., initialization list in a class'
Packit ea8578
 * constructor).
Packit ea8578
 *
Packit ea8578
 * @code
Packit ea8578
 * struct json_object_iterator iter = json_object_iter_init_default();
Packit ea8578
 * MyClass() : iter_(json_object_iter_init_default())
Packit ea8578
 * @endcode
Packit ea8578
 *
Packit ea8578
 * @note The initialized value doesn't reference any specific
Packit ea8578
 *       pair, is considered an invalid iterator, and MUST NOT
Packit ea8578
 *       be passed to any json-c API that expects a valid
Packit ea8578
 *       iterator.
Packit ea8578
 *
Packit ea8578
 * @note User and internal code MUST NOT make any assumptions
Packit ea8578
 *       about and dependencies on the value of the "default"
Packit ea8578
 *       iterator value.
Packit ea8578
 *
Packit ea8578
 * @return json_object_iterator
Packit ea8578
 */
Packit ea8578
struct json_object_iterator
Packit ea8578
json_object_iter_init_default(void);
Packit ea8578
Packit ea8578
/** Retrieves an iterator to the first pair of the JSON Object.
Packit ea8578
 *
Packit ea8578
 * @warning 	Any modification of the underlying pair invalidates all
Packit ea8578
 * 		iterators to that pair.
Packit ea8578
 *
Packit ea8578
 * @param obj	JSON Object instance (MUST be of type json_object)
Packit ea8578
 *
Packit ea8578
 * @return json_object_iterator If the JSON Object has at
Packit ea8578
 *              least one pair, on return, the iterator refers
Packit ea8578
 *              to the first pair. If the JSON Object doesn't
Packit ea8578
 *              have any pairs, the returned iterator is
Packit ea8578
 *              equivalent to the "end" iterator for the same
Packit ea8578
 *              JSON Object instance.
Packit ea8578
 *
Packit ea8578
 * @code
Packit ea8578
 * struct json_object_iterator it;
Packit ea8578
 * struct json_object_iterator itEnd;
Packit ea8578
 * struct json_object* obj;
Packit ea8578
 *
Packit ea8578
 * obj = json_tokener_parse("{'first':'george', 'age':100}");
Packit ea8578
 * it = json_object_iter_begin(obj);
Packit ea8578
 * itEnd = json_object_iter_end(obj);
Packit ea8578
 *
Packit ea8578
 * while (!json_object_iter_equal(&it, &itEnd)) {
Packit ea8578
 *     printf("%s\n",
Packit ea8578
 *            json_object_iter_peek_name(&it);;
Packit ea8578
 *     json_object_iter_next(&it);
Packit ea8578
 * }
Packit ea8578
 *
Packit ea8578
 * @endcode
Packit ea8578
 */
Packit ea8578
struct json_object_iterator
Packit ea8578
json_object_iter_begin(struct json_object* obj);
Packit ea8578
Packit ea8578
/** Retrieves the iterator that represents the position beyond the
Packit ea8578
 *  last pair of the given JSON Object instance.
Packit ea8578
 *
Packit ea8578
 *  @warning Do NOT write code that assumes that the "end"
Packit ea8578
 *        iterator value is NULL, even if it is so in a
Packit ea8578
 *        particular instance of the implementation.
Packit ea8578
 *
Packit ea8578
 *  @note The reason we do not (and MUST NOT) provide
Packit ea8578
 *        "json_object_iter_is_end(json_object_iterator* iter)"
Packit ea8578
 *        type of API is because it would limit the underlying
Packit ea8578
 *        representation of name/value containment (or force us
Packit ea8578
 *        to add additional, otherwise unnecessary, fields to
Packit ea8578
 *        the iterator structure). The "end" iterator and the
Packit ea8578
 *        equality test method, on the other hand, permit us to
Packit ea8578
 *        cleanly abstract pretty much any reasonable underlying
Packit ea8578
 *        representation without burdening the iterator
Packit ea8578
 *        structure with unnecessary data.
Packit ea8578
 *
Packit ea8578
 *  @note For performance reasons, memorize the "end" iterator prior
Packit ea8578
 *        to any loop.
Packit ea8578
 *
Packit ea8578
 * @param obj JSON Object instance (MUST be of type json_object)
Packit ea8578
 *
Packit ea8578
 * @return json_object_iterator On return, the iterator refers
Packit ea8578
 *              to the "end" of the Object instance's pairs
Packit ea8578
 *              (i.e., NOT the last pair, but "beyond the last
Packit ea8578
 *              pair" value)
Packit ea8578
 */
Packit ea8578
struct json_object_iterator
Packit ea8578
json_object_iter_end(const struct json_object* obj);
Packit ea8578
Packit ea8578
/** Returns an iterator to the next pair, if any
Packit ea8578
 *
Packit ea8578
 * @warning	Any modification of the underlying pair
Packit ea8578
 *       	invalidates all iterators to that pair.
Packit ea8578
 *
Packit ea8578
 * @param iter [IN/OUT] Pointer to iterator that references a
Packit ea8578
 *         name/value pair; MUST be a valid, non-end iterator.
Packit ea8578
 *         WARNING: bad things will happen if invalid or "end"
Packit ea8578
 *         iterator is passed. Upon return will contain the
Packit ea8578
 *         reference to the next pair if there is one; if there
Packit ea8578
 *         are no more pairs, will contain the "end" iterator
Packit ea8578
 *         value, which may be compared against the return value
Packit ea8578
 *         of json_object_iter_end() for the same JSON Object
Packit ea8578
 *         instance.
Packit ea8578
 */
Packit ea8578
void
Packit ea8578
json_object_iter_next(struct json_object_iterator* iter);
Packit ea8578
Packit ea8578
Packit ea8578
/** Returns a const pointer to the name of the pair referenced
Packit ea8578
 *  by the given iterator.
Packit ea8578
 *
Packit ea8578
 * @param iter pointer to iterator that references a name/value
Packit ea8578
 *             pair; MUST be a valid, non-end iterator.
Packit ea8578
 *
Packit ea8578
 * @warning	bad things will happen if an invalid or
Packit ea8578
 *             	"end" iterator is passed.
Packit ea8578
 *
Packit ea8578
 * @return const char* Pointer to the name of the referenced
Packit ea8578
 *         name/value pair.  The name memory belongs to the
Packit ea8578
 *         name/value pair, will be freed when the pair is
Packit ea8578
 *         deleted or modified, and MUST NOT be modified or
Packit ea8578
 *         freed by the user.
Packit ea8578
 */
Packit ea8578
const char*
Packit ea8578
json_object_iter_peek_name(const struct json_object_iterator* iter);
Packit ea8578
Packit ea8578
Packit ea8578
/** Returns a pointer to the json-c instance representing the
Packit ea8578
 *  value of the referenced name/value pair, without altering
Packit ea8578
 *  the instance's reference count.
Packit ea8578
 *
Packit ea8578
 * @param iter 	pointer to iterator that references a name/value
Packit ea8578
 *             	pair; MUST be a valid, non-end iterator.
Packit ea8578
 *
Packit ea8578
 * @warning	bad things will happen if invalid or
Packit ea8578
 *             "end" iterator is passed.
Packit ea8578
 *
Packit ea8578
 * @return struct json_object* Pointer to the json-c value
Packit ea8578
 *         instance of the referenced name/value pair;  the
Packit ea8578
 *         value's reference count is not changed by this
Packit ea8578
 *         function: if you plan to hold on to this json-c node,
Packit ea8578
 *         take a look at json_object_get() and
Packit ea8578
 *         json_object_put(). IMPORTANT: json-c API represents
Packit ea8578
 *         the JSON Null value as a NULL json_object instance
Packit ea8578
 *         pointer.
Packit ea8578
 */
Packit ea8578
struct json_object*
Packit ea8578
json_object_iter_peek_value(const struct json_object_iterator* iter);
Packit ea8578
Packit ea8578
Packit ea8578
/** Tests two iterators for equality.  Typically used to test
Packit ea8578
 *  for end of iteration by comparing an iterator to the
Packit ea8578
 *  corresponding "end" iterator (that was derived from the same
Packit ea8578
 *  JSON Object instance).
Packit ea8578
 *
Packit ea8578
 *  @note The reason we do not (and MUST NOT) provide
Packit ea8578
 *        "json_object_iter_is_end(json_object_iterator* iter)"
Packit ea8578
 *        type of API is because it would limit the underlying
Packit ea8578
 *        representation of name/value containment (or force us
Packit ea8578
 *        to add additional, otherwise unnecessary, fields to
Packit ea8578
 *        the iterator structure). The equality test method, on
Packit ea8578
 *        the other hand, permits us to cleanly abstract pretty
Packit ea8578
 *        much any reasonable underlying representation.
Packit ea8578
 *
Packit ea8578
 * @param iter1 Pointer to first valid, non-NULL iterator
Packit ea8578
 * @param iter2 POinter to second valid, non-NULL iterator
Packit ea8578
 *
Packit ea8578
 * @warning	if a NULL iterator pointer or an uninitialized
Packit ea8578
 *       	or invalid iterator, or iterators derived from
Packit ea8578
 *       	different JSON Object instances are passed, bad things
Packit ea8578
 *       	will happen!
Packit ea8578
 *
Packit ea8578
 * @return json_bool non-zero if iterators are equal (i.e., both
Packit ea8578
 *         reference the same name/value pair or are both at
Packit ea8578
 *         "end"); zero if they are not equal.
Packit ea8578
 */
Packit ea8578
json_bool
Packit ea8578
json_object_iter_equal(const struct json_object_iterator* iter1,
Packit ea8578
                       const struct json_object_iterator* iter2);
Packit ea8578
Packit ea8578
Packit ea8578
#ifdef __cplusplus
Packit ea8578
}
Packit ea8578
#endif
Packit ea8578
Packit ea8578
Packit ea8578
#endif /* JSON_OBJECT_ITERATOR_H */