Change kore_json_find() to operate on a kore_json_item.

This way you can call the lookup function on any JSON value that you
previously obtained (or the JSON context root).
This commit is contained in:
Joris Vink 2019-10-25 12:27:16 +02:00
parent f637d617aa
commit 5f03f991c9
3 changed files with 13 additions and 23 deletions

View File

@ -32,12 +32,12 @@ page(struct http_request *req)
if (!kore_json_parse(&json)) { if (!kore_json_parse(&json)) {
kore_buf_appendf(&buf, "%s\n", kore_json_strerror(&json)); kore_buf_appendf(&buf, "%s\n", kore_json_strerror(&json));
} else { } else {
if ((item = kore_json_string(&json, "foo/bar")) != NULL) { item = kore_json_find_string(json.root, "foo/bar");
if (item != NULL) {
kore_buf_appendf(&buf, kore_buf_appendf(&buf,
"foo.bar = '%s'\n", item->data.string); "foo.bar = '%s'\n", item->data.string);
} else { } else {
kore_buf_appendf(&buf, "%s\n", kore_buf_appendf(&buf, "string foo.bar not found\n");
kore_json_strerror(&json));
} }
} }

View File

@ -966,7 +966,8 @@ void kore_json_init(struct kore_json *, const u_int8_t *, size_t);
void kore_json_item_tobuf(struct kore_json_item *, struct kore_buf *); void kore_json_item_tobuf(struct kore_json_item *, struct kore_buf *);
const char *kore_json_strerror(struct kore_json *); const char *kore_json_strerror(struct kore_json *);
struct kore_json_item *kore_json_find(struct kore_json *, const char *, int); struct kore_json_item *kore_json_find(struct kore_json_item *,
const char *, int);
struct kore_json_item *kore_json_create_item(struct kore_json_item *, struct kore_json_item *kore_json_create_item(struct kore_json_item *,
const char *, int, ...); const char *, int, ...);

View File

@ -41,8 +41,8 @@ static int json_parse_literal(struct kore_json *, struct kore_json_item *);
static struct kore_json_item *json_item_alloc(int, const char *, static struct kore_json_item *json_item_alloc(int, const char *,
struct kore_json_item *); struct kore_json_item *);
static struct kore_json_item *json_find_item(struct kore_json *, static struct kore_json_item *json_find_item(struct kore_json_item *,
struct kore_json_item *, char **, int, int); char **, int, int);
static u_int8_t json_null_literal[] = { 'n', 'u', 'l', 'l' }; static u_int8_t json_null_literal[] = { 'n', 'u', 'l', 'l' };
static u_int8_t json_true_literal[] = { 't', 'r', 'u', 'e' }; static u_int8_t json_true_literal[] = { 't', 'r', 'u', 'e' };
@ -103,7 +103,7 @@ kore_json_parse(struct kore_json *json)
} }
struct kore_json_item * struct kore_json_item *
kore_json_find(struct kore_json *json, const char *path, int type) kore_json_find(struct kore_json_item *root, const char *path, int type)
{ {
struct kore_json_item *item; struct kore_json_item *item;
char *copy; char *copy;
@ -116,13 +116,9 @@ kore_json_find(struct kore_json *json, const char *path, int type)
return (NULL); return (NULL);
} }
json->error = 0; item = json_find_item(root, tokens, type, 0);
item = json_find_item(json, json->root, tokens, type, 0);
kore_free(copy); kore_free(copy);
if (item == NULL && json->error == 0)
json->error = KORE_JSON_ERR_NOT_FOUND;
return (item); return (item);
} }
@ -254,8 +250,7 @@ kore_json_item_tobuf(struct kore_json_item *item, struct kore_buf *buf)
} }
static struct kore_json_item * static struct kore_json_item *
json_find_item(struct kore_json *json, struct kore_json_item *object, json_find_item(struct kore_json_item *object, char **tokens, int type, int pos)
char **tokens, int type, int pos)
{ {
char *p, *str; char *p, *str;
struct kore_json_item *item, *nitem; struct kore_json_item *item, *nitem;
@ -271,18 +266,14 @@ json_find_item(struct kore_json *json, struct kore_json_item *object,
if ((str = strchr(tokens[pos], '[')) != NULL) { if ((str = strchr(tokens[pos], '[')) != NULL) {
*(str)++ = '\0'; *(str)++ = '\0';
if ((p = strchr(str, ']')) == NULL) { if ((p = strchr(str, ']')) == NULL)
json->error = KORE_JSON_ERR_INVALID_SEARCH;
return (NULL); return (NULL);
}
*p = '\0'; *p = '\0';
spot = kore_strtonum(str, 10, 0, USHRT_MAX, &err); spot = kore_strtonum(str, 10, 0, USHRT_MAX, &err);
if (err != KORE_RESULT_OK) { if (err != KORE_RESULT_OK)
json->error = KORE_JSON_ERR_INVALID_SEARCH;
return (NULL); return (NULL);
}
} else { } else {
spot = -1; spot = -1;
} }
@ -310,14 +301,12 @@ json_find_item(struct kore_json *json, struct kore_json_item *object,
if (tokens[pos + 1] == NULL) { if (tokens[pos + 1] == NULL) {
if (item->type == type) if (item->type == type)
return (item); return (item);
json->error = KORE_JSON_ERR_TYPE_MISMATCH;
return (NULL); return (NULL);
} }
if (item->type == KORE_JSON_TYPE_OBJECT || if (item->type == KORE_JSON_TYPE_OBJECT ||
item->type == KORE_JSON_TYPE_ARRAY) { item->type == KORE_JSON_TYPE_ARRAY) {
item = json_find_item(json, item = json_find_item(item, tokens, type, pos + 1);
item, tokens, type, pos + 1);
} else { } else {
item = NULL; item = NULL;
} }