Annotation of libelwix/example/test_json.c, revision 1.1.2.6

1.1.2.1   misho       1: #include <stdio.h>
                      2: #include <string.h>
                      3: #include <unistd.h>
                      4: #include <fcntl.h>
1.1.2.5   misho       5: #include <errno.h>
1.1.2.1   misho       6: #include <sys/types.h>
                      7: #include <elwix.h>
                      8: 
                      9: 
                     10: int
                     11: run_simple(json_t *json)
                     12: {
1.1.2.4   misho      13:        static const char *teststr = "{\"user\": \"johndoe\", { \"meow\", \"\", \"\aaa\"}, \"admin\": false, \"uid\": 1000, \"objects\" : { \"a\":\"1\", \"b\" : \"2\" ,\"c\" : \"3\", , \"oho\"},\n"
                     14:                                        "\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}";
1.1.2.1   misho      15:        int i, ret;
1.1.2.4   misho      16:        jtok_t *tok, toks[36];
1.1.2.1   misho      17:        ait_val_t *v;
                     18:        char *str;
                     19:        array_t *arr;
                     20: 
                     21:        json_init(json, 0);
                     22:        ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
                     23:        if (ret == -1) {
                     24:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     25:                return 1;
                     26:        } else
                     27:                printf("We are need from %d tokens\n", ret);
                     28:        json_init(json, 1);
                     29:        ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
                     30:        if (ret == -1) {
                     31:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     32:                return 1;
                     33:        } else
                     34:                printf("(strict) We are need from %d tokens\n", ret);
                     35:        json_init(json, 0);
                     36:        ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
                     37:        if (ret == -1) {
                     38:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     39:                return 1;
                     40:        } else
1.1.2.2   misho      41:                printf("We parsed %d tokens next=%lu\n", ret, json->h_next);
1.1.2.1   misho      42:        memset(toks, 0, sizeof toks);
                     43:        json_init(json, 1);
                     44:        ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
                     45:        if (ret == -1) {
                     46:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     47:                return 1;
                     48:        } else
1.1.2.2   misho      49:                printf("(strict) We parsed %d tokens next=%lu\n", ret, json->h_next);
1.1.2.1   misho      50: 
                     51:        if (!(tok = json_findbykey(teststr, "boza s kosmi", toks, ret)))
                     52:                printf("Key=\"boza s kosmi\" not found!\n");
                     53:        tok = json_findbykey(teststr, "user", toks, ret);
                     54:        v = json_token2val(teststr, tok);
1.1.2.4   misho      55:        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));
1.1.2.1   misho      56:        ait_freeVar(&v);
                     57:        tok = json_findbykey(teststr, "admin", toks, ret);
                     58:        str = json_token2str(teststr, tok);
1.1.2.4   misho      59:        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);
1.1.2.1   misho      60:        e_free(str);
                     61:        tok = json_findbykey(teststr, "uid", toks, ret);
1.1.2.4   misho      62:        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, 
1.1.2.1   misho      63:                        json_token2num(teststr, tok));
                     64:        arr = json_token2array(teststr, tok);
                     65:        for (i = 0; i < array_Size(arr); i++)
                     66:                printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
                     67:        ait_freeVars(&arr);
                     68: 
                     69:        tok = json_findbykey(teststr, "groups", toks, ret);
                     70:        str = json_token2str(teststr, tok);
                     71:        arr = json_token2array(teststr, tok);
1.1.2.4   misho      72:        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);
                     73:        e_free(str);
                     74:        for (i = 0; i < array_Size(arr); i++)
                     75:                printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
                     76:        ait_freeVars(&arr);
                     77: 
                     78:        tok = json_findbykey(teststr, "objects", toks, ret);
                     79:        str = json_token2str(teststr, tok);
                     80:        arr = json_token2array(teststr, tok);
                     81:        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);
                     82:        e_free(str);
                     83:        for (i = 0; i < array_Size(arr); i++)
                     84:                printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
                     85:        ait_freeVars(&arr);
                     86: 
                     87:        tok = json_findbykey(teststr, "testend", toks, ret);
                     88:        str = json_token2str(teststr, tok);
                     89:        arr = json_token2array(teststr, tok);
                     90:        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);
1.1.2.1   misho      91:        e_free(str);
                     92:        for (i = 0; i < array_Size(arr); i++)
                     93:                printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
                     94:        ait_freeVars(&arr);
                     95: 
                     96:        return 0;
                     97: }
                     98: 
1.1.2.6 ! misho      99: static int
        !           100: dump(const char *jstr, jtok_t *toks, int toksnum, int indent)
        !           101: {
        !           102:        register int i, j, k;
        !           103: 
        !           104:        if (!toksnum)
        !           105:                return 0;
        !           106: 
        !           107:        if (toks->tok_type == J_VALUE) {
        !           108:                printf("%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
        !           109:                return 1;
        !           110:        } else if (toks->tok_type == J_STRING) {
        !           111:                printf("%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
        !           112:                return 1;
        !           113:        } else if (toks->tok_type == J_OBJECT) {
        !           114:                printf("\n");
        !           115:                for (j = i = 0; i < json_toksize(toks); i++) {
        !           116:                        for (k = 0; k < indent; k++)
        !           117:                                printf("  ");
        !           118:                        j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
        !           119:                        printf(": ");
        !           120:                        j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
        !           121:                        printf("\n");
        !           122:                }
        !           123:                return j + 1;
        !           124:        } else if (toks->tok_type == J_ARRAY) {
        !           125:                printf("\n");
        !           126:                for (j = i = 0; i < json_toksize(toks); i++) {
        !           127:                        for (k = 0; k < indent - 1; k++)
        !           128:                                printf("  ");
        !           129:                        printf("   - ");
        !           130:                        j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
        !           131:                        printf("\n");
        !           132:                }
        !           133:                return j + 1;
        !           134:        }
        !           135: 
        !           136:        return 0;
        !           137: }
        !           138: 
1.1.2.1   misho     139: int
1.1.2.5   misho     140: run_dump(json_t *json)
                    141: {
                    142:        int rlen, toksnum, wait4eof = 0;
                    143:        char buf[BUFSIZ];
                    144:        ait_val_t v;
                    145:        jtok_t *toks, *t;
                    146: 
                    147:        toks = e_malloc(sizeof(jtok_t));
                    148:        if (!toks)
                    149:                return 1;
                    150:        else
                    151:                toksnum = 1;
                    152:        AIT_SET_STRSIZ(&v, 1);
                    153:        while (42) {
                    154:                memset(buf, 0, sizeof buf);
                    155:                rlen = fread(buf, 1, sizeof buf, stdin);
                    156:                switch (rlen) {
                    157:                        case -1:
                    158:                                printf("Error:: fread() #%d - %s\n", errno, strerror(errno));
                    159:                                AIT_FREE_VAL(&v);
                    160:                                e_free(toks);
                    161:                                return 2;
                    162:                        case 0:
                    163:                                if (wait4eof)
                    164:                                        return 0;
                    165:                                printf("Error:: Unexpected termination!\n");
                    166:                                AIT_FREE_VAL(&v);
                    167:                                e_free(toks);
                    168:                                return 3;
                    169:                        default:
                    170:                                AIT_SET_STRCAT(&v, buf);
                    171:                                break;
                    172:                }
                    173: again:
                    174:                rlen = json_parse(json, AIT_GET_STR(&v), AIT_LEN(&v), toks, toksnum);
                    175:                if (rlen == (u_int) -1) {
                    176:                        if (elwix_GetErrno() == J_ERR_NOMEM) {
                    177:                                toksnum *= 2;
                    178:                                t = e_realloc(toks, sizeof(jtok_t) * toksnum);
                    179:                                if (t) {
                    180:                                        toks = t;
                    181:                                        goto again;
                    182:                                }
                    183:                        }
                    184:                } else {
                    185:                        dump(AIT_GET_STR(&v), toks, json->h_next, 0);
                    186:                        wait4eof = 1;
                    187:                }
                    188:        }
                    189:        AIT_FREE_VAL(&v);
                    190:        e_free(toks);
                    191: 
                    192:        return 0;
                    193: }
                    194: 
                    195: int
1.1.2.1   misho     196: main(int argc, char **argv)
                    197: {
                    198:        json_t json;
                    199: 
                    200:        if (argc < 2)
                    201:                return run_simple(&json);
1.1.2.5   misho     202:        else
                    203:                return run_dump(&json);
1.1.2.1   misho     204: 
                    205:        return 0;
                    206: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>