Skip to content

Commit

Permalink
Fix json iterator example code
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyGaul committed Jun 27, 2024
1 parent 26faa50 commit 9d75125
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
30 changes: 18 additions & 12 deletions docs/json/cf_json_iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,29 @@ val | The JSON value to iterate upon.
Returns a [CF_JIter](/json/cf_jiter.md) for iterating.
## Code Example
> Traversing arrays/objects.
```cpp
// Traverse an array of strings:
for (CF_JIter i = cf_json_iter(v); !cf_json_iter_done(i); i = cf_json_iter_next(i)) {
const char val = cf_json_get_string(cf_json_iter_val(i));
printf("%s\n", val);
}
// Traverse key/val pairs on an objects:
for (CF_JIter i = cf_json_iter(v); !cf_json_iter_done(i); iter = cf_json_iter_next(i)) {
const char val = cf_json_get_string(cf_json_iter_val(i));
printf("%s\n", val);
}
```

## Remarks

The [CF_JIter](/json/cf_jiter.md) can be used in foor loops, and can traverse both JSON arrays and objects. When
traversing arrays do not call [cf_json_iter_key](/json/cf_json_iter_key.md).

// Traverse an array of strings:
for (CF_JIter i = cf_json_iter(v); !cf_json_iter_done(i); i = cf_json_iter_next(i)) {
const char val = cf_json_get_string(cf_json_iter_val(i));
printf("%s\n", val);
}
// Traverse key/val pairs on an objects:
for (CF_JIter i = cf_json_iter(v); !cf_json_iter_done(i); iter = cf_json_iter_next(i)) {
const char val = cf_json_get_string(cf_json_iter_val(i));
printf("%s\n", val);
}
## Related Pages

[CF_JVal](/json/cf_jval.md)
Expand Down
6 changes: 3 additions & 3 deletions include/cute_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ typedef struct CF_JIter
* @brief Creates an iterator for a given JSON value.
* @param val The JSON value to iterate upon.
* @return Returns a `CF_JIter` for iterating.
* @remarks The `CF_JIter` can be used in foor loops, and can traverse both JSON arrays and objects. When
* traversing arrays do not call `cf_json_iter_key`.
*
* @example > Traversing arrays/objects.
* // Traverse an array of strings:
* for (CF_JIter i = cf_json_iter(v); !cf_json_iter_done(i); i = cf_json_iter_next(i)) {
* const char* val = cf_json_get_string(cf_json_iter_val(i));
Expand All @@ -344,6 +342,8 @@ typedef struct CF_JIter
* const char* val = cf_json_get_string(cf_json_iter_val(i));
* printf("%s\n", val);
* }
* @remarks The `CF_JIter` can be used in foor loops, and can traverse both JSON arrays and objects. When
* traversing arrays do not call `cf_json_iter_key`.
* @related CF_JVal cf_json_get cf_json_array_at cf_json_array_get cf_json_iter cf_json_iter_remove
*/
CF_API CF_JIter CF_CALL cf_json_iter(CF_JVal val);
Expand Down

0 comments on commit 9d75125

Please sign in to comment.