coreJSON v3.3.0
Parser library for the ECMA-404 JSON standard
 
Loading...
Searching...
No Matches
JSON_Iterate
JSONStatus_t JSON_Iterate( const char * buf,
size_t max,
size_t * start,
size_t * next,
JSONPair_t * outPair );
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:1802
JSONStatus_t
Return codes from coreJSON library functions.
Definition: core_json.h:57
Structure to represent a key-value pair.
Definition: core_json.h:259

Output the next key-value pair or value from a collection.

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]bufThe buffer to search.
[in]maxsize of the buffer.
[in,out]startThe index at which the collection begins.
[in,out]nextThe index at which to seek the next value.
[out]outPairA 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

// Variables used in this 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;
JSONPair_t pair = { 0 };
JSONStatus_t result;
result = JSON_Validate( json, length );
if( result == JSONSuccess )
{
result = JSON_Iterate( json, length, &start, &next, &pair );
}
while( result == JSONSuccess )
{
if( pair.key != NULL )
{
printf( "key: %.*s\t", ( int ) pair.keyLength, pair.key );
}
printf( "value: (%s) %.*s\n", json_types[ pair.jsonType ],
( int ) pair.valueLength, pair.value );
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:1160
@ JSONSuccess
JSON document is valid and complete.
Definition: core_json.h:59
const char * value
Pointer to the code point sequence for value.
Definition: core_json.h:262
size_t keyLength
Length of the code point sequence for key.
Definition: core_json.h:261
JSONTypes_t jsonType
JSON-specific type of the value.
Definition: core_json.h:264
size_t valueLength
Length of the code point sequence for value.
Definition: core_json.h:263
const char * key
Pointer to the code point sequence for key.
Definition: core_json.h:260

See core_json.h for docs.