File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_json.c
Revision 1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Tue Nov 28 02:00:45 2017 UTC (6 years, 6 months ago) by misho
Branches: elwix4_5
start replace code with macros

    1: #include <stdio.h>
    2: #include <string.h>
    3: #include <unistd.h>
    4: #include <fcntl.h>
    5: #include <sys/types.h>
    6: #include <elwix.h>
    7: 
    8: 
    9: int
   10: run_simple(json_t *json)
   11: {
   12: 	static const char *teststr = "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n  "
   13:         				"\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
   14: 	int i, ret;
   15: 	jtok_t *tok, toks[20];
   16: 	ait_val_t *v;
   17: 	char *str;
   18: 	array_t *arr;
   19: 
   20: 	json_init(json, 0);
   21: 	ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
   22: 	if (ret == -1) {
   23: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   24: 		return 1;
   25: 	} else
   26: 		printf("We are need from %d tokens\n", ret);
   27: 	json_init(json, 1);
   28: 	ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
   29: 	if (ret == -1) {
   30: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   31: 		return 1;
   32: 	} else
   33: 		printf("(strict) We are need from %d tokens\n", ret);
   34: 	json_init(json, 0);
   35: 	ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
   36: 	if (ret == -1) {
   37: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   38: 		return 1;
   39: 	} else
   40: 		printf("We parsed %d tokens next=%lu\n", ret, json->h_next);
   41: 	memset(toks, 0, sizeof toks);
   42: 	json_init(json, 1);
   43: 	ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
   44: 	if (ret == -1) {
   45: 		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
   46: 		return 1;
   47: 	} else
   48: 		printf("(strict) We parsed %d tokens next=%lu\n", ret, json->h_next);
   49: 
   50: 	if (!(tok = json_findbykey(teststr, "boza s kosmi", toks, ret)))
   51: 		printf("Key=\"boza s kosmi\" not found!\n");
   52: 	tok = json_findbykey(teststr, "user", toks, ret);
   53: 	v = json_token2val(teststr, tok);
   54: 	printf("Key=\"user\" data size=%ld type=%d %s\n", tok->tok_size, tok->tok_type, AIT_GET_STR(v));
   55: 	ait_freeVar(&v);
   56: 	tok = json_findbykey(teststr, "admin", toks, ret);
   57: 	str = json_token2str(teststr, tok);
   58: 	printf("Key=\"admin\" data size=%ld type=%d %s\n", tok->tok_size, tok->tok_type, str);
   59: 	e_free(str);
   60: 	tok = json_findbykey(teststr, "uid", toks, ret);
   61: 	printf("Key=\"uid\" data size=%ld type=%d %ld\n", tok->tok_size, tok->tok_type, 
   62: 			json_token2num(teststr, tok));
   63: 	arr = json_token2array(teststr, tok);
   64: 	for (i = 0; i < array_Size(arr); i++)
   65: 		printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
   66: 	ait_freeVars(&arr);
   67: 
   68: 	tok = json_findbykey(teststr, "groups", toks, ret);
   69: 	str = json_token2str(teststr, tok);
   70: 	arr = json_token2array(teststr, tok);
   71: 	printf("Key=\"groups\" data size=%ld type=%d %s\n", tok->tok_size, tok->tok_type, str);
   72: 	e_free(str);
   73: 	for (i = 0; i < array_Size(arr); i++)
   74: 		printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
   75: 	ait_freeVars(&arr);
   76: 
   77: 	return 0;
   78: }
   79: 
   80: int
   81: main(int argc, char **argv)
   82: {
   83: 	json_t json;
   84: 
   85: 	if (argc < 2)
   86: 		return run_simple(&json);
   87: 
   88: 	return 0;
   89: }

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