version 1.1.2.1, 2017/11/28 00:46:53
|
version 1.5, 2024/12/04 17:47:28
|
Line 2
|
Line 2
|
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
#include <fcntl.h> |
#include <fcntl.h> |
|
#include <errno.h> |
#include <sys/types.h> |
#include <sys/types.h> |
#include <elwix.h> |
#include <elwix.h> |
|
|
Line 9
|
Line 10
|
int |
int |
run_simple(json_t *json) |
run_simple(json_t *json) |
{ |
{ |
static const char *teststr = "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\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\"]}"; | "\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}"; |
| // static const char *teststr = "{}\n[]\n\"sdfsdf\""; |
int i, ret; |
int i, ret; |
jtok_t *tok, toks[20]; | jtok_t *tok, toks[36]; |
ait_val_t *v; |
ait_val_t *v; |
char *str; |
char *str; |
array_t *arr; |
array_t *arr; |
Line 37 run_simple(json_t *json)
|
Line 39 run_simple(json_t *json)
|
printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError()); |
printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError()); |
return 1; |
return 1; |
} else |
} else |
printf("We parsed %d tokens\n", ret); | printf("We parsed %d tokens next=%lu\n", ret, json->h_next); |
memset(toks, 0, sizeof toks); |
memset(toks, 0, sizeof toks); |
json_init(json, 1); |
json_init(json, 1); |
ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks); |
ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks); |
Line 45 run_simple(json_t *json)
|
Line 47 run_simple(json_t *json)
|
printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError()); |
printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError()); |
return 1; |
return 1; |
} else |
} else |
printf("(strict) We parsed %d tokens\n", ret); | 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); |
v = json_token2val(teststr, tok); | if (tok) { |
printf("Key=\"user\" data size=%ld type=%d %s\n", tok->tok_size, tok->tok_type, AIT_GET_STR(v)); | v = json_token2val(teststr, tok); |
ait_freeVar(&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)); |
tok = json_findbykey(teststr, "admin", toks, ret); | ait_freeVar(&v); |
str = json_token2str(teststr, tok); | } |
printf("Key=\"admin\" data size=%ld type=%d %s\n", tok->tok_size, tok->tok_type, str); | tok = json_findbykey(teststr, "admin", J_VALUE, toks, ret); |
e_free(str); | if (tok) { |
tok = json_findbykey(teststr, "uid", toks, ret); | str = json_token2str(teststr, tok); |
printf("Key=\"uid\" data size=%ld type=%d %ld\n", tok->tok_size, tok->tok_type, | 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); |
json_token2num(teststr, tok)); | e_free(str); |
arr = json_token2array(teststr, tok); | } |
for (i = 0; i < array_Size(arr); i++) | tok = json_findbykey(teststr, "uid", J_UNDEF, toks, ret); |
printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*))); | if (tok) { |
ait_freeVars(&arr); | 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)); |
| arr = json_token2array(teststr, tok); |
| for (i = 0; i < array_Size(arr); i++) |
| printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*))); |
| ait_freeVars(&arr); |
| } |
|
|
tok = json_findbykey(teststr, "groups", toks, ret); | tok = json_findbykey(teststr, "groups", J_UNDEF, toks, ret); |
str = json_token2str(teststr, tok); | if (tok) { |
arr = json_token2array(teststr, tok); | str = json_token2str(teststr, tok); |
printf("Key=\"groups\" data size=%ld type=%d %s\n", tok->tok_size, tok->tok_type, str); | arr = json_token2array(teststr, tok); |
e_free(str); | printf("Key=\"groups\" data parent=%ld idx=%ld size=%ld type=%d %s\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, str); |
for (i = 0; i < array_Size(arr); i++) | e_free(str); |
printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*))); | for (i = 0; i < array_Size(arr); i++) |
| printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*))); |
| ait_freeVars(&arr); |
| } |
| |
| tok = json_findbykey(teststr, "objects", J_UNDEF, toks, ret); |
| if (tok) { |
| str = json_token2str(teststr, tok); |
| arr = json_token2array(teststr, tok); |
| printf("Key=\"objects\" 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); |
| for (i = 0; i < array_Size(arr); i++) |
| printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*))); |
| ait_freeVars(&arr); |
| } |
| |
| tok = json_findbykey(teststr, "testend", J_UNDEF, toks, ret); |
| if (tok) { |
| str = json_token2str(teststr, tok); |
| arr = json_token2array(teststr, tok); |
| printf("Key=\"testend\" 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); |
| for (i = 0; i < array_Size(arr); i++) |
| printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*))); |
| ait_freeVars(&arr); |
| } |
| |
| return 0; |
| } |
| |
| static int |
| dump(const char *jstr, jtok_t *toks, int toksnum, int indent) |
| { |
| register int i, j, k; |
| |
| if (!toksnum) |
| return 0; |
| |
| if (toks->tok_type == J_VALUE) { |
| printf("%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks)); |
| return 1; |
| } else if (toks->tok_type == J_STRING) { |
| printf("%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks)); |
| return 1; |
| } else if (toks->tok_type == J_OBJECT) { |
| printf("\n"); |
| for (j = i = 0; i < json_toksize(toks); i++) { |
| for (k = 0; k < indent; k++) |
| printf(" "); |
| j += dump(jstr, toks + j + 1, toksnum - j, indent + 1); |
| printf(": "); |
| j += dump(jstr, toks + j + 1, toksnum - j, indent + 1); |
| printf("\n"); |
| } |
| return j + 1; |
| } else if (toks->tok_type == J_ARRAY) { |
| printf("\n"); |
| for (j = i = 0; i < json_toksize(toks); i++) { |
| for (k = 0; k < indent - 1; k++) |
| printf(" "); |
| printf(" - "); |
| j += dump(jstr, toks + j + 1, toksnum - j, indent + 1); |
| printf("\n"); |
| } |
| return j + 1; |
| } |
| |
| return 0; |
| } |
| |
| int |
| run_dump(json_t *json, char **args) |
| { |
| int rlen, toksnum, wait4eof = 0; |
| char buf[BUFSIZ]; |
| ait_val_t v; |
| jtok_t *toks, *t; |
| FILE *f; |
| |
| json_init(json, 0); |
| f = fopen(args[1], "r"); |
| if (!f) { |
| printf("fopen(%s) #%d - %s\n", *args, errno, strerror(errno)); |
| return 1; |
| } |
| |
| toks = e_malloc(sizeof(jtok_t)); |
| if (!toks) { |
| fclose(f); |
| return 1; |
| } else |
| toksnum = 1; |
| AIT_SET_STRSIZ(&v, 1); |
| while (42) { |
| memset(buf, 0, sizeof buf); |
| rlen = fread(buf, 1, sizeof buf, f); |
| switch (rlen) { |
| case -1: |
| printf("Error:: fread() #%d - %s\n", errno, strerror(errno)); |
| AIT_FREE_VAL(&v); |
| e_free(toks); |
| fclose(f); |
| return 2; |
| case 0: |
| if (wait4eof) |
| return 0; |
| printf("Error:: Unexpected termination!\n"); |
| AIT_FREE_VAL(&v); |
| e_free(toks); |
| fclose(f); |
| return 3; |
| default: |
| AIT_SET_STRCAT(&v, buf); |
| break; |
| } |
| again: |
| rlen = json_parse(json, AIT_GET_STR(&v), AIT_LEN(&v), toks, toksnum); |
| if (rlen == (u_int) -1) { |
| if (elwix_GetErrno() == J_ERR_NOMEM) { |
| toksnum *= 2; |
| t = e_realloc(toks, sizeof(jtok_t) * toksnum); |
| if (t) { |
| toks = t; |
| goto again; |
| } |
| } |
| } else { |
| dump(AIT_GET_STR(&v), toks, json->h_next, 0); |
| wait4eof = 1; |
| } |
| } |
| AIT_FREE_VAL(&v); |
| e_free(toks); |
| fclose(f); |
| |
| return 0; |
| } |
| |
| 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); |
ait_freeVars(&arr); |
|
|
|
printf("%s\n", str); |
return 0; |
return 0; |
} |
} |
|
|
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_stdin(&json); |
|
else if (argc < 3) |
return run_simple(&json); |
return run_simple(&json); |
|
else if (argc < 4) |
|
return run_make(); |
|
else |
|
return run_dump(&json, argv); |
|
|
return 0; |
return 0; |
} |
} |