File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_json.c
Revision 1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Tue Nov 28 02:05:21 2017 UTC (6 years, 5 months ago) by misho
Branches: elwix4_5
change

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <elwix.h>


int
run_simple(json_t *json)
{
	static const char *teststr = "{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n  "
        				"\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"]}";
	int i, ret;
	jtok_t *tok, toks[20];
	ait_val_t *v;
	char *str;
	array_t *arr;

	json_init(json, 0);
	ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
	if (ret == -1) {
		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
		return 1;
	} else
		printf("We are need from %d tokens\n", ret);
	json_init(json, 1);
	ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
	if (ret == -1) {
		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
		return 1;
	} else
		printf("(strict) We are need from %d tokens\n", ret);
	json_init(json, 0);
	ret = json_parse(json, teststr, strlen(teststr), 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);
	memset(toks, 0, sizeof toks);
	json_init(json, 1);
	ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
	if (ret == -1) {
		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
		return 1;
	} else
		printf("(strict) We parsed %d tokens next=%lu\n", ret, json->h_next);

	if (!(tok = json_findbykey(teststr, "boza s kosmi", toks, ret)))
		printf("Key=\"boza s kosmi\" not found!\n");
	tok = json_findbykey(teststr, "user", toks, ret);
	v = json_token2val(teststr, tok);
	printf("Key=\"user\" data size=%ld type=%d %s\n", tok->tok_size, tok->tok_type, AIT_GET_STR(v));
	ait_freeVar(&v);
	tok = json_findbykey(teststr, "admin", toks, ret);
	str = json_token2str(teststr, tok);
	printf("Key=\"admin\" data size=%ld type=%d %s\n", tok->tok_size, tok->tok_type, str);
	e_free(str);
	tok = json_findbykey(teststr, "uid", toks, ret);
	printf("Key=\"uid\" data size=%ld type=%d %ld\n", 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);
	str = json_token2str(teststr, tok);
	arr = json_token2array(teststr, tok);
	printf("Key=\"groups\" data size=%ld type=%d %s\n", 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;
}

int
main(int argc, char **argv)
{
	json_t json;

	if (argc < 2)
		return run_simple(&json);

	return 0;
}

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