version 1.1.2.9, 2017/11/30 13:45:38
|
version 1.5, 2024/12/04 17:47:28
|
Line 11 int
|
Line 11 int
|
run_simple(json_t *json) |
run_simple(json_t *json) |
{ |
{ |
static const char *teststr = "{\"user\": \"johndoe\", { \"meow\", \"\", \"\aaa\"}, \"admin\": false, \"uid\": 1000, \"objects\" : { \"a\":\"1\", \"b\" : \"2\" ,\"c\" : \"3\", , \"oho\"},\n" |
static const char *teststr = "{\"user\": \"johndoe\", { \"meow\", \"\", \"\aaa\"}, \"admin\": false, \"uid\": 1000, \"objects\" : { \"a\":\"1\", \"b\" : \"2\" ,\"c\" : \"3\", , \"oho\"},\n" |
"\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}"; | "\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}"; |
// static const char *teststr = "{}\n[]\n\"sdfsdf\""; |
// static const char *teststr = "{}\n[]\n\"sdfsdf\""; |
int i, ret; |
int i, ret; |
jtok_t *tok, toks[36]; |
jtok_t *tok, toks[36]; |
Line 49 run_simple(json_t *json)
|
Line 49 run_simple(json_t *json)
|
} else |
} else |
printf("(strict) We parsed %d tokens next=%lu\n", ret, json->h_next); |
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"); |
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) { |
if (tok) { |
v = json_token2val(teststr, 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)); |
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); |
ait_freeVar(&v); |
} |
} |
tok = json_findbykey(teststr, "admin", toks, ret); | tok = json_findbykey(teststr, "admin", J_VALUE, toks, ret); |
if (tok) { |
if (tok) { |
str = json_token2str(teststr, 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); |
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); |
e_free(str); |
} |
} |
tok = json_findbykey(teststr, "uid", toks, ret); | tok = json_findbykey(teststr, "uid", J_UNDEF, toks, ret); |
if (tok) { |
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, |
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)); |
json_token2num(teststr, tok)); |
Line 73 run_simple(json_t *json)
|
Line 73 run_simple(json_t *json)
|
ait_freeVars(&arr); |
ait_freeVars(&arr); |
} |
} |
|
|
tok = json_findbykey(teststr, "groups", toks, ret); | tok = json_findbykey(teststr, "groups", J_UNDEF, toks, ret); |
if (tok) { |
if (tok) { |
str = json_token2str(teststr, tok); |
str = json_token2str(teststr, tok); |
arr = json_token2array(teststr, tok); |
arr = json_token2array(teststr, tok); |
Line 84 run_simple(json_t *json)
|
Line 84 run_simple(json_t *json)
|
ait_freeVars(&arr); |
ait_freeVars(&arr); |
} |
} |
|
|
tok = json_findbykey(teststr, "objects", toks, ret); | tok = json_findbykey(teststr, "objects", J_UNDEF, toks, ret); |
if (tok) { |
if (tok) { |
str = json_token2str(teststr, tok); |
str = json_token2str(teststr, tok); |
arr = json_token2array(teststr, tok); |
arr = json_token2array(teststr, tok); |
Line 95 run_simple(json_t *json)
|
Line 95 run_simple(json_t *json)
|
ait_freeVars(&arr); |
ait_freeVars(&arr); |
} |
} |
|
|
tok = json_findbykey(teststr, "testend", toks, ret); | tok = json_findbykey(teststr, "testend", J_UNDEF, toks, ret); |
if (tok) { |
if (tok) { |
str = json_token2str(teststr, tok); |
str = json_token2str(teststr, tok); |
arr = json_token2array(teststr, tok); |
arr = json_token2array(teststr, tok); |
Line 258 run_make()
|
Line 258 run_make()
|
} |
} |
|
|
int |
int |
|
run_stdin(json_t *json) |
|
{ |
|
char szJSON[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; |
|
jtok_t *tok, toks[1024]; |
|
int ret, scope; |
|
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); |
|
} |
|
} |
|
|
|
printf("Default scope=%ld, scope for \"zzz\"=%ld\n", |
|
json_objscope(NULL, szJSON, toks, ret), |
|
(scope = json_objscope("zzz", szJSON, toks, ret))); |
|
|
|
tok = json_findbykeyatscope(0, szJSON, "t1", J_UNDEF, toks, ret); |
|
if (tok) { |
|
str = json_token2str(szJSON, tok); |
|
if (str) { |
|
printf("default scope t1=%s\n", str); |
|
json_freestr(str); |
|
} |
|
} |
|
tok = json_findbykeyatscope(scope, szJSON, "t1", J_UNDEF, toks, ret); |
|
if (tok) { |
|
str = json_token2str(szJSON, tok); |
|
if (str) { |
|
printf("scope=%ld object zzz t1=%s\n", scope, str); |
|
json_freestr(str); |
|
} |
|
} |
|
|
|
scope = json_objscope("z", szJSON, toks, ret); |
|
tok = json_findbykeyatscope(scope, szJSON, "t1", J_UNDEF, toks, ret); |
|
if (tok) { |
|
str = json_token2str(szJSON, tok); |
|
if (str) { |
|
printf("scope=%ld object z t1=%s\n", scope, str); |
|
json_freestr(str); |
|
} |
|
} |
|
|
|
return 0; |
|
} |
|
|
|
int |
main(int argc, char **argv) |
main(int argc, char **argv) |
{ |
{ |
json_t json; |
json_t json; |
|
|
if (argc < 2) |
if (argc < 2) |
return run_simple(&json); | return run_stdin(&json); |
else if (argc < 3) |
else if (argc < 3) |
|
return run_simple(&json); |
|
else if (argc < 4) |
return run_make(); |
return run_make(); |
else |
else |
return run_dump(&json, argv); |
return run_dump(&json, argv); |