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

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"
1.1.2.7   misho      14:                                        "\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}";
                     15: //     static const char *teststr = "{}\n[]\n\"sdfsdf\"";
1.1.2.1   misho      16:        int i, ret;
1.1.2.4   misho      17:        jtok_t *tok, toks[36];
1.1.2.1   misho      18:        ait_val_t *v;
                     19:        char *str;
                     20:        array_t *arr;
                     21: 
                     22:        json_init(json, 0);
                     23:        ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
                     24:        if (ret == -1) {
                     25:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     26:                return 1;
                     27:        } else
                     28:                printf("We are need from %d tokens\n", ret);
                     29:        json_init(json, 1);
                     30:        ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
                     31:        if (ret == -1) {
                     32:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     33:                return 1;
                     34:        } else
                     35:                printf("(strict) We are need from %d tokens\n", ret);
                     36:        json_init(json, 0);
                     37:        ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
                     38:        if (ret == -1) {
                     39:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     40:                return 1;
                     41:        } else
1.1.2.2   misho      42:                printf("We parsed %d tokens next=%lu\n", ret, json->h_next);
1.1.2.1   misho      43:        memset(toks, 0, sizeof toks);
                     44:        json_init(json, 1);
                     45:        ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
                     46:        if (ret == -1) {
                     47:                printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
                     48:                return 1;
                     49:        } else
1.1.2.2   misho      50:                printf("(strict) We parsed %d tokens next=%lu\n", ret, json->h_next);
1.1.2.1   misho      51: 
                     52:        if (!(tok = json_findbykey(teststr, "boza s kosmi", toks, ret)))
                     53:                printf("Key=\"boza s kosmi\" not found!\n");
                     54:        tok = json_findbykey(teststr, "user", toks, ret);
1.1.2.7   misho      55:        if (tok) {
                     56:                v = json_token2val(teststr, tok);
                     57:                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));
                     58:                ait_freeVar(&v);
                     59:        }
1.1.2.1   misho      60:        tok = json_findbykey(teststr, "admin", toks, ret);
1.1.2.7   misho      61:        if (tok) {
                     62:                str = json_token2str(teststr, tok);
                     63:                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);
                     64:                e_free(str);
                     65:        }
1.1.2.1   misho      66:        tok = json_findbykey(teststr, "uid", toks, ret);
1.1.2.7   misho      67:        if (tok) {
                     68:                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, 
                     69:                                json_token2num(teststr, tok));
                     70:                arr = json_token2array(teststr, tok);
                     71:                for (i = 0; i < array_Size(arr); i++)
                     72:                        printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
                     73:                ait_freeVars(&arr);
                     74:        }
1.1.2.1   misho      75: 
                     76:        tok = json_findbykey(teststr, "groups", toks, ret);
1.1.2.7   misho      77:        if (tok) {
                     78:                str = json_token2str(teststr, tok);
                     79:                arr = json_token2array(teststr, tok);
                     80:                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);
                     81:                e_free(str);
                     82:                for (i = 0; i < array_Size(arr); i++)
                     83:                        printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
                     84:                ait_freeVars(&arr);
                     85:        }
1.1.2.4   misho      86: 
                     87:        tok = json_findbykey(teststr, "objects", toks, ret);
1.1.2.7   misho      88:        if (tok) {
                     89:                str = json_token2str(teststr, tok);
                     90:                arr = json_token2array(teststr, tok);
                     91:                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);
                     92:                e_free(str);
                     93:                for (i = 0; i < array_Size(arr); i++)
                     94:                        printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
                     95:                ait_freeVars(&arr);
                     96:        }
1.1.2.4   misho      97: 
                     98:        tok = json_findbykey(teststr, "testend", toks, ret);
1.1.2.7   misho      99:        if (tok) {
                    100:                str = json_token2str(teststr, tok);
                    101:                arr = json_token2array(teststr, tok);
                    102:                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);
                    103:                e_free(str);
                    104:                for (i = 0; i < array_Size(arr); i++)
                    105:                        printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
                    106:                ait_freeVars(&arr);
                    107:        }
1.1.2.1   misho     108: 
                    109:        return 0;
                    110: }
                    111: 
1.1.2.6   misho     112: static int
                    113: dump(const char *jstr, jtok_t *toks, int toksnum, int indent)
                    114: {
                    115:        register int i, j, k;
                    116: 
                    117:        if (!toksnum)
                    118:                return 0;
                    119: 
                    120:        if (toks->tok_type == J_VALUE) {
                    121:                printf("%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
                    122:                return 1;
                    123:        } else if (toks->tok_type == J_STRING) {
                    124:                printf("%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
                    125:                return 1;
                    126:        } else if (toks->tok_type == J_OBJECT) {
                    127:                printf("\n");
                    128:                for (j = i = 0; i < json_toksize(toks); i++) {
                    129:                        for (k = 0; k < indent; k++)
                    130:                                printf("  ");
                    131:                        j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
                    132:                        printf(": ");
                    133:                        j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
                    134:                        printf("\n");
                    135:                }
                    136:                return j + 1;
                    137:        } else if (toks->tok_type == J_ARRAY) {
                    138:                printf("\n");
                    139:                for (j = i = 0; i < json_toksize(toks); i++) {
                    140:                        for (k = 0; k < indent - 1; k++)
                    141:                                printf("  ");
                    142:                        printf("   - ");
                    143:                        j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
                    144:                        printf("\n");
                    145:                }
                    146:                return j + 1;
                    147:        }
                    148: 
                    149:        return 0;
                    150: }
                    151: 
1.1.2.1   misho     152: int
1.1.2.7   misho     153: run_dump(json_t *json, char **args)
1.1.2.5   misho     154: {
                    155:        int rlen, toksnum, wait4eof = 0;
                    156:        char buf[BUFSIZ];
                    157:        ait_val_t v;
                    158:        jtok_t *toks, *t;
1.1.2.7   misho     159:        FILE *f;
                    160: 
                    161:        json_init(json, 0);
                    162:        f = fopen(args[1], "r");
                    163:        if (!f) {
                    164:                printf("fopen(%s) #%d - %s\n", *args, errno, strerror(errno));
1.1.2.8   misho     165:                return 1;
1.1.2.7   misho     166:        }
1.1.2.5   misho     167: 
                    168:        toks = e_malloc(sizeof(jtok_t));
1.1.2.7   misho     169:        if (!toks) {
                    170:                fclose(f);
1.1.2.5   misho     171:                return 1;
1.1.2.7   misho     172:        } else
1.1.2.5   misho     173:                toksnum = 1;
                    174:        AIT_SET_STRSIZ(&v, 1);
                    175:        while (42) {
                    176:                memset(buf, 0, sizeof buf);
1.1.2.7   misho     177:                rlen = fread(buf, 1, sizeof buf, f);
1.1.2.5   misho     178:                switch (rlen) {
                    179:                        case -1:
                    180:                                printf("Error:: fread() #%d - %s\n", errno, strerror(errno));
                    181:                                AIT_FREE_VAL(&v);
                    182:                                e_free(toks);
1.1.2.7   misho     183:                                fclose(f);
1.1.2.5   misho     184:                                return 2;
                    185:                        case 0:
                    186:                                if (wait4eof)
                    187:                                        return 0;
                    188:                                printf("Error:: Unexpected termination!\n");
                    189:                                AIT_FREE_VAL(&v);
                    190:                                e_free(toks);
1.1.2.7   misho     191:                                fclose(f);
1.1.2.5   misho     192:                                return 3;
                    193:                        default:
                    194:                                AIT_SET_STRCAT(&v, buf);
                    195:                                break;
                    196:                }
                    197: again:
                    198:                rlen = json_parse(json, AIT_GET_STR(&v), AIT_LEN(&v), toks, toksnum);
                    199:                if (rlen == (u_int) -1) {
                    200:                        if (elwix_GetErrno() == J_ERR_NOMEM) {
                    201:                                toksnum *= 2;
                    202:                                t = e_realloc(toks, sizeof(jtok_t) * toksnum);
                    203:                                if (t) {
                    204:                                        toks = t;
                    205:                                        goto again;
                    206:                                }
                    207:                        }
                    208:                } else {
                    209:                        dump(AIT_GET_STR(&v), toks, json->h_next, 0);
                    210:                        wait4eof = 1;
                    211:                }
                    212:        }
                    213:        AIT_FREE_VAL(&v);
                    214:        e_free(toks);
1.1.2.7   misho     215:        fclose(f);
1.1.2.5   misho     216: 
                    217:        return 0;
                    218: }
                    219: 
                    220: int
1.1.2.9 ! misho     221: run_make()
        !           222: {
        !           223:        char str[BUFSIZ];
        !           224:        ait_val_t *v;
        !           225:        array_t *arr;
        !           226: 
        !           227:        arr = ait_allocVars(4);
        !           228:        v = ait_getVars(&arr, 0);
        !           229:        AIT_SET_I16(v, 4444);
        !           230:        v = ait_getVars(&arr, 1);
        !           231:        AIT_SET_STR(v, "sezam");
        !           232:        v = ait_getVars(&arr, 2);
        !           233:        AIT_SET_U8(v, 0x1c);
        !           234:        v = ait_getVars(&arr, 3);
        !           235:        AIT_SET_STR(v, "0x45af");
        !           236: 
        !           237:        json_add_begin_object(str, sizeof str, 42);
        !           238:        json_add_pair(str, sizeof str, 42, "boza", "s kosmi");
        !           239:        json_add_comma(str, sizeof str, 42);
        !           240:        json_add_string(str, sizeof str, 0, "number");
        !           241:        json_add_colon(str, sizeof str, 42);
        !           242:        json_add_value(str, sizeof str, 0, 1234567890);
        !           243:        json_add_comma(str, sizeof str, 42);
        !           244:        json_add_string(str, sizeof str, 0, "obj__");
        !           245:        json_add_colon(str, sizeof str, 42);
        !           246:        json_add_begin_object(str, sizeof str, 42);
        !           247:        json_add_pair(str, sizeof str, 42, "subkey", "val0");
        !           248:        json_add_end_object(str, sizeof str, 42);
        !           249:        json_add_comma(str, sizeof str, 42);
        !           250:        json_add_string(str, sizeof str, 0, "arr4e");
        !           251:        json_add_colon(str, sizeof str, 42);
        !           252:        json_add_array(str, sizeof str, 42, arr);
        !           253:        json_add_end_object(str, sizeof str, 42);
        !           254:        ait_freeVars(&arr);
        !           255: 
        !           256:        printf("%s\n", str);
        !           257:        return 0;
        !           258: }
        !           259: 
        !           260: int
1.1.2.1   misho     261: main(int argc, char **argv)
                    262: {
                    263:        json_t json;
                    264: 
                    265:        if (argc < 2)
                    266:                return run_simple(&json);
1.1.2.9 ! misho     267:        else if (argc < 3)
        !           268:                return run_make();
1.1.2.5   misho     269:        else
1.1.2.7   misho     270:                return run_dump(&json, argv);
1.1.2.1   misho     271: 
                    272:        return 0;
                    273: }

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