--- libelwix/example/test_json.c 2017/11/28 15:06:04 1.1.2.8 +++ libelwix/example/test_json.c 2021/03/21 01:32:04 1.4 @@ -49,21 +49,21 @@ run_simple(json_t *json) } else printf("(strict) We parsed %d tokens next=%lu\n", ret, json->h_next); - if (!(tok = json_findbykey(teststr, "boza s kosmi", toks, ret))) + if (!(tok = json_findbykey(teststr, "boza s kosmi", J_UNDEF, toks, ret))) printf("Key=\"boza s kosmi\" not found!\n"); - tok = json_findbykey(teststr, "user", toks, ret); + tok = json_findbykey(teststr, "user", J_UNDEF, toks, ret); if (tok) { v = json_token2val(teststr, tok); printf("Key=\"user\" data parent=%ld idx=%ld size=%ld type=%d %s\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, AIT_GET_STR(v)); ait_freeVar(&v); } - tok = json_findbykey(teststr, "admin", toks, ret); + tok = json_findbykey(teststr, "admin", J_VALUE, toks, ret); if (tok) { str = json_token2str(teststr, tok); printf("Key=\"admin\" data parent=%ld idx=%ld size=%ld type=%d %s\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, str); e_free(str); } - tok = json_findbykey(teststr, "uid", toks, ret); + tok = json_findbykey(teststr, "uid", J_UNDEF, toks, ret); if (tok) { printf("Key=\"uid\" data parent=%ld idx=%ld size=%ld type=%d %ld\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, json_token2num(teststr, tok)); @@ -73,7 +73,7 @@ run_simple(json_t *json) ait_freeVars(&arr); } - tok = json_findbykey(teststr, "groups", toks, ret); + tok = json_findbykey(teststr, "groups", J_UNDEF, toks, ret); if (tok) { str = json_token2str(teststr, tok); arr = json_token2array(teststr, tok); @@ -84,7 +84,7 @@ run_simple(json_t *json) ait_freeVars(&arr); } - tok = json_findbykey(teststr, "objects", toks, ret); + tok = json_findbykey(teststr, "objects", J_UNDEF, toks, ret); if (tok) { str = json_token2str(teststr, tok); arr = json_token2array(teststr, tok); @@ -95,7 +95,7 @@ run_simple(json_t *json) ait_freeVars(&arr); } - tok = json_findbykey(teststr, "testend", toks, ret); + tok = json_findbykey(teststr, "testend", J_UNDEF, toks, ret); if (tok) { str = json_token2str(teststr, tok); arr = json_token2array(teststr, tok); @@ -218,12 +218,89 @@ again: } int +run_make() +{ + char str[BUFSIZ]; + ait_val_t *v; + array_t *arr; + + arr = ait_allocVars(4); + v = ait_getVars(&arr, 0); + AIT_SET_I16(v, 4444); + v = ait_getVars(&arr, 1); + AIT_SET_STR(v, "sezam"); + v = ait_getVars(&arr, 2); + AIT_SET_U8(v, 0x1c); + v = ait_getVars(&arr, 3); + AIT_SET_STR(v, "0x45af"); + + json_add_begin_object(str, sizeof str, 42); + json_add_pair(str, sizeof str, 42, "boza", "s kosmi"); + json_add_comma(str, sizeof str, 42); + json_add_string(str, sizeof str, 0, "number"); + json_add_colon(str, sizeof str, 42); + json_add_value(str, sizeof str, 0, 1234567890); + json_add_comma(str, sizeof str, 42); + json_add_string(str, sizeof str, 0, "obj__"); + json_add_colon(str, sizeof str, 42); + json_add_begin_object(str, sizeof str, 42); + json_add_pair(str, sizeof str, 42, "subkey", "val0"); + json_add_end_object(str, sizeof str, 42); + json_add_comma(str, sizeof str, 42); + json_add_string(str, sizeof str, 0, "arr4e"); + json_add_colon(str, sizeof str, 42); + json_add_array(str, sizeof str, 42, arr); + json_add_end_object(str, sizeof str, 42); + ait_freeVars(&arr); + + printf("%s\n", str); + return 0; +} + +int +run_stdin(json_t *json) +{ + char szJSON[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; + jtok_t *tok, toks[1024]; + int ret; + char *str; + + printf("Input JSON> "); + fgets(szJSON, sizeof szJSON, stdin); + printf("Readed JSON> %s", szJSON); + + json_init(json, 0); + ret = json_parse(json, szJSON, strlen(szJSON), toks, sizeof toks/sizeof *toks); + if (ret == -1) { + printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError()); + return 1; + } else + printf("We parsed %d tokens next=%lu\n", ret, json->h_next); + + printf("dump=%d\n", json_dump(stdout, szJSON, toks, json->h_next, 0)); + + tok = json_findbykey(szJSON, "aaa", J_STRING, toks, ret); + if (tok) { + str = json_token2str(szJSON, tok); + if (str) { + printf("str=%s\n", str); + json_freestr(str); + } + } + return 0; +} + +int main(int argc, char **argv) { json_t json; if (argc < 2) + return run_stdin(&json); + else if (argc < 3) return run_simple(&json); + else if (argc < 4) + return run_make(); else return run_dump(&json, argv);