Output the next key-value pair or value from a collection.
size_t max,
size_t * start,
size_t * next,
JSONStatus_t JSON_Iterate(const char *buf, size_t max, size_t *start, size_t *next, JSONPair_t *outPair)
Output the next key-value pair or value from a collection.
Definition: core_json.c:1755
JSONStatus_t
Return codes from coreJSON library functions.
Definition: core_json.h:39
Structure to represent a key-value pair.
Definition: core_json.h:241
This function may be used in a loop to output each key-value pair from an object, or each value from an array. For the first invocation, the integers pointed to by start and next should be initialized to 0. These will be updated by the function. If another key-value pair or value is present, the output structure is populated and JSONSuccess is returned; otherwise the structure is unchanged and JSONNotFound is returned.
- Parameters
-
[in] | buf | The buffer to search. |
[in] | max | size of the buffer. |
[in,out] | start | The index at which the collection begins. |
[in,out] | next | The index at which to seek the next value. |
[out] | outPair | A pointer to receive the next key-value pair. |
- Note
- This function expects a valid JSON document; run JSON_Validate() first.
-
For an object, the outPair structure will reference a key and its value. For an array, only the value will be referenced (i.e., outPair.key will be NULL).
- Returns
- JSONSuccess if a value is output; JSONIllegalDocument if the buffer does not contain a collection; JSONNotFound if there are no further values in the collection.
Example
static char * json_types[] =
{
"invalid",
"string",
"number",
"true",
"false",
"null",
"object",
"array"
};
void show( const char * json,
size_t length )
{
size_t start = 0, next = 0;
{
result =
JSON_Iterate( json, length, &start, &next, &pair );
}
{
{
}
printf(
"value: (%s) %.*s\n", json_types[ pair.
jsonType ],
result =
JSON_Iterate( json, length, &start, &next, &pair );
}
}
JSONStatus_t JSON_Validate(const char *buf, size_t max)
Parse a buffer to determine if it contains a valid JSON document.
Definition: core_json.c:1118
@ JSONSuccess
JSON document is valid and complete.
Definition: core_json.h:41
const char * value
Pointer to the code point sequence for value.
Definition: core_json.h:244
size_t keyLength
Length of the code point sequence for key.
Definition: core_json.h:243
JSONTypes_t jsonType
JSON-specific type of the value.
Definition: core_json.h:246
size_t valueLength
Length of the code point sequence for value.
Definition: core_json.h:245
const char * key
Pointer to the code point sequence for key.
Definition: core_json.h:242
See core_json.h for docs.