chibipub

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 7e273f2e59a9dc7cc7408264e80f19f6c719c6ed
parent 1402b081bb2e991d5a12ad41ee8464009cfa2477
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  7 Dec 2020 18:00:03 -0800

wip

Diffstat:
Msrc/json.c | 24+++++++++++++++++-------
Msrc/json.h | 2+-
Msrc/test_json.c | 7+++----
3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/json.c b/src/json.c @@ -5,19 +5,26 @@ #include <ctype.h> -#define note_error(p, msg, ...) fprintf(stderr, "%s: " msg "\n", __FUNCTION__, ##__VA_ARGS__); +#define note_error(p, msg, ...) note_error_(p, "%s: " msg "\n", __FUNCTION__, ##__VA_ARGS__); + +struct errors { + int record_errors; +} struct json_parser { struct cursor cur; struct cursor ubjson; - int record_errors; + struct errors errors; }; -/* -static void note_error_(struct json_parser *p, ...) +static void note_error_(struct json_parser *p, const char *fmt, ...) { + if (!p->errors.record_errors) + return; + + + int record_errors; } -*/ static void consume_whitespace(struct cursor *cur) { @@ -584,7 +591,7 @@ static int ubjson_obj_lookup(struct ubjson *ubjson, const char *path, struct jso return 0; } -int read_ubjson_object(struct ubjson *ubjson, const char **path, int path_len, struct json *val) +int ubjson_lookup(struct ubjson *ubjson, const char **path, int path_len, struct json *val) { int i; const char *seg; @@ -619,7 +626,10 @@ void print_value(struct json *val) { switch (val->type) { case JSON_STRING: - printf("%.*s\n", val->len, val->string); + printf("%.*s", val->len, val->string); + return; + case JSON_NULL: + printf("null"); return; default: printf("implement print %d", val->type); diff --git a/src/json.h b/src/json.h @@ -32,7 +32,7 @@ struct json { int print_ubjson(struct ubjson *json); int parse_json(unsigned char *buf, size_t buf_size, struct ubjson *out); -int read_ubjson_object(struct ubjson *ubjson, const char **path, int path_len, struct json *val); +int ubjson_lookup(struct ubjson *ubjson, const char **path, int path_len, struct json *val); void print_value(struct json *val); #endif diff --git a/src/test_json.c b/src/test_json.c @@ -30,12 +30,11 @@ int main(int argc, char *argv[]) assert(parse_json(empty, sizeof(empty), &ubjson)); ubjson.cur.p = ubjson.cur.start; - unsigned char kv[] = "{ \"key\" : {\"a\": \"b\"} , \"otherkey\": \"stringy\"}"; + unsigned char kv[] = "{ \"key\" : {\"obj\":\"123\"} , \"otherkey\": \"stringy\"}"; assert(parse_json(kv, sizeof(kv), &ubjson)); - printf("\n"); - const char *path[] = {"key", "a"}; - assert(read_ubjson_object(&ubjson, path, 2, &val)); + const char *path[] = {"key", "obj"}; + assert(ubjson_lookup(&ubjson, path, 2, &val)); printf("found val: "); print_value(&val);