Skip to content

Commit

Permalink
json_parse -> json_walk
Browse files Browse the repository at this point in the history
PUBLISHED_FROM=595bbd2accccca3b16482ee6963d65cb8d43e2d0
  • Loading branch information
cpq authored and cesantabot committed Jul 14, 2016
1 parent 3603c6a commit e195625
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/api/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ title: API Reference
items:
- { name: json_scanf.md }
- { name: json_printf.md }
- { name: json_parse.md }
- { name: json_walk.md }
---
14 changes: 7 additions & 7 deletions docs/api/json_parse.md → docs/api/json_walk.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
---
title: json_parse()
title: json_walk()
---

```c
/* Callback-based API */
typedef void (*json_parse_callback_t)(void *callback_data, const char *path,
const struct json_token *token);
typedef void (*json_walk_callback_t)(void *callback_data, const char *path,
const struct json_token *token);

/*
* Parse `json_string`, invoking `callback` function for each JSON token.
* Return number of bytes processed
*/
int json_parse(const char *json_string, int json_string_length,
json_parse_callback_t callback, void *callback_data);
int json_walk(const char *json_string, int json_string_length,
json_walk_callback_t callback, void *callback_data);
```
`json_parse()` is a low-level, callback based parsing API.
`json_parse()` calls given callback function for each scanned value.
`json_walk()` is a low-level, callback based parsing API.
`json_walk()` calls given callback function for each scanned value.
Callback receives a path to the value, a JSON token that points to the value,
and arbitrary user data pointer.
Expand Down
10 changes: 5 additions & 5 deletions frozen.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct frozen {
char path[JSON_MAX_PATH_LEN];
int path_len;
void *callback_data;
json_parse_callback_t callback;
json_walk_callback_t callback;
};

struct fstate {
Expand Down Expand Up @@ -599,8 +599,8 @@ int cs_win_snprintf(char *str, size_t size, const char *format, ...) {
}
#endif /* _WIN32 */

int json_parse(const char *json_string, int json_string_length,
json_parse_callback_t callback, void *callback_data) {
int json_walk(const char *json_string, int json_string_length,
json_walk_callback_t callback, void *callback_data) {
struct frozen frozen;

memset(&frozen, 0, sizeof(frozen));
Expand Down Expand Up @@ -633,7 +633,7 @@ int json_scanf_array_elem(const char *s, int len, const char *path, int idx,
info.token = token;
memset(token, 0, sizeof(*token));
snprintf(info.path, sizeof(info.path), "%s[%d]", path, idx);
json_parse(s, len, json_scanf_array_elem_cb, &info);
json_walk(s, len, json_scanf_array_elem_cb, &info);
return token->len;
}

Expand Down Expand Up @@ -720,7 +720,7 @@ int json_vscanf(const char *s, int len, const char *fmt, va_list ap) {
break;
}
}
json_parse(s, len, json_scanf_cb, &info);
json_walk(s, len, json_scanf_cb, &info);
} else if (is_alpha(fmt[i]) || get_utf8_char_len(fmt[i]) > 1) {
const char *delims = ": \r\n\t";
int key_len = strcspn(&fmt[i], delims);
Expand Down
8 changes: 4 additions & 4 deletions frozen.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ struct json_token {
#define JSON_STRING_INCOMPLETE -2

/* Callback-based API */
typedef void (*json_parse_callback_t)(void *callback_data, const char *path,
const struct json_token *token);
typedef void (*json_walk_callback_t)(void *callback_data, const char *path,
const struct json_token *token);

/*
* Parse `json_string`, invoking `callback` function for each JSON token.
* Return number of bytes processed
*/
int json_parse(const char *json_string, int json_string_length,
json_parse_callback_t callback, void *callback_data);
int json_walk(const char *json_string, int json_string_length,
json_walk_callback_t callback, void *callback_data);

/*
* JSON generation API.
Expand Down
22 changes: 11 additions & 11 deletions unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,24 @@ static const char *test_errors(void) {
" e : null, f: [ 1, -2, 3], g: { \"1\": [], h: [ 7 ] } } ";
int i;

ASSERT(json_parse(NULL, 0, NULL, 0) == JSON_STRING_INVALID);
ASSERT(json_walk(NULL, 0, NULL, 0) == JSON_STRING_INVALID);
for (i = 0; invalid_tests[i] != NULL; i++) {
ASSERT(json_parse(invalid_tests[i], strlen(invalid_tests[i]), NULL, NULL) ==
ASSERT(json_walk(invalid_tests[i], strlen(invalid_tests[i]), NULL, NULL) ==
JSON_STRING_INVALID);
}

for (i = 0; incomplete_tests[i] != NULL; i++) {
ASSERT(json_parse(incomplete_tests[i], strlen(incomplete_tests[i]), NULL,
NULL) == JSON_STRING_INCOMPLETE);
ASSERT(json_walk(incomplete_tests[i], strlen(incomplete_tests[i]), NULL,
NULL) == JSON_STRING_INCOMPLETE);
}

for (i = 0; success_tests[i].str != NULL; i++) {
ASSERT(json_parse(success_tests[i].str, strlen(success_tests[i].str), NULL,
NULL) == success_tests[i].expected_len);
ASSERT(json_walk(success_tests[i].str, strlen(success_tests[i].str), NULL,
NULL) == success_tests[i].expected_len);
}

ASSERT(json_parse("{}", 2, NULL, NULL) == 2);
ASSERT(json_parse(s1, strlen(s1), NULL, 0) > 0);
ASSERT(json_walk("{}", 2, NULL, NULL) == 2);
ASSERT(json_walk(s1, strlen(s1), NULL, 0) > 0);

return NULL;
}
Expand Down Expand Up @@ -284,7 +284,7 @@ static const char *test_callback_api() {
"2->.c[0].a[9] 1->.c[0].b[x] 3->.c[0][{\"a\":9,\"b\":\"x\"}] "
"7->.c[[{\"a\":9,\"b\":\"x\"}]] 3->[{\"c\":[{\"a\":9,\"b\":\"x\"}]}] ";
char buf[200] = "";
ASSERT(json_parse(s, strlen(s), cb, buf) == (int) strlen(s));
ASSERT(json_walk(s, strlen(s), cb, buf) == (int) strlen(s));
ASSERT(strcmp(buf, result) == 0);
return NULL;
}
Expand Down Expand Up @@ -317,9 +317,9 @@ static const char *test_scanf(void) {
{
/* Test errors */
const char *str = "{foo:1, bar:[2,3,4]}";
ASSERT(json_parse(str, strlen(str), NULL, NULL) == (int) strlen(str));
ASSERT(json_walk(str, strlen(str), NULL, NULL) == (int) strlen(str));
for (size_t i = 1; i < strlen(str); i++) {
ASSERT(json_parse(str, i, NULL, NULL) == JSON_STRING_INCOMPLETE);
ASSERT(json_walk(str, i, NULL, NULL) == JSON_STRING_INCOMPLETE);
}
}

Expand Down

0 comments on commit e195625

Please sign in to comment.