--- libelwix/inc/elwix/ajson.h 2017/11/24 10:29:15 1.1.2.2 +++ libelwix/inc/elwix/ajson.h 2017/12/03 21:50:23 1.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: ajson.h,v 1.1.2.2 2017/11/24 10:29:15 misho Exp $ +* $Id: ajson.h,v 1.3 2017/12/03 21:50:23 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -53,9 +53,7 @@ typedef enum { J_OBJECT = 1, J_ARRAY = 2, J_STRING = 3, - J_NUMBER = 4, - J_BOOL = 5, - J_NULL = 6 + J_VALUE = 4 } jtype_t; /* JSON error numbers */ @@ -63,40 +61,43 @@ typedef enum { J_ERR_OK = 0, J_ERR_NOMEM = 1, J_ERR_INVAL = 2, - J_ERR_PART = 3 + J_ERR_PART = 3, + J_ERR_PARAM = 4 } jerr_t; -/* JSON error strings */ -typedef const char jerrstr[] = { - "No error", - "Not enough tokens were provided", - "Invalid character", - "JSON string isn't full", - NULL -}; - -typedef struct _tagParser { - unsigned long pos; - unsigned long next; - long parent; +typedef struct _tagHandler { + unsigned long h_pos; + unsigned long h_next; + long h_parent; + void *h_alloc; + int h_strict; } json_t; +#define json_toksnum(x) (x)->h_next typedef struct _tagToken { + long tok_idx; jtype_t tok_type; long tok_start; long tok_end; long tok_size; long tok_parent; } jtok_t; +#define json_toktype(x) (x)->tok_type +#define json_toksize(x) (x)->tok_size +#define json_toklen(x) ((x)->tok_end - (x)->tok_start) +#define json_tokstr(j, x) ((j) + (x)->tok_start) +#define json_tokstrcpy(d, j, x) { strncpy((d), json_tokstr((j), (x)), json_toklen(x)); \ + (d)[json_toklen(x)] = 0; } /* * json_init() - Initialize JSON handler * * @json = JSON handler, if there is NULL then dynamically will be allocated - * return: =NULL error or !=NULL ready for use JSON handler. + * @jstrict = JSON strict mode, when we select strict mode every unquoted value is error + * return: =NULL error or !=NULL ready for use JSON handler and should be free with json_free() */ -json_t *json_init(json_t * __restrict json); +json_t *json_init(json_t * __restrict json, int jstrict); /* * json_free() - Free JSON handler @@ -104,7 +105,7 @@ json_t *json_init(json_t * __restrict json); * @json = JSON handler * return: none */ -void json_free(json_t **json); +void json_free(json_t * __restrict json); /* * json_parse() - Parse JSON string @@ -118,6 +119,173 @@ void json_free(json_t **json); */ unsigned int json_parse(json_t * __restrict json, const char *jstr, size_t jlen, jtok_t * __restrict jtoks, unsigned int toksnum); + +/* + * json_token2val() - Return token to AIT variable + * + * @jstr = JSON string + * @tok = Token for convert + * @return =NULL error or !=NULL variable, after use should be ait_freeVar() + */ +ait_val_t *json_token2val(const char *jstr, jtok_t * __restrict tok); +/* + * json_token2str() - Return token to string + * + * @jstr = JSON string + * @tok = Token for convert + * @return =NULL error or !=NULL allocated str, after use should be e_free() + */ +char *json_token2str(const char *jstr, jtok_t * __restrict tok); +/* + * json_token2num() - Return token to numeric + * + * @jstr = JSON string + * @tok = Token for convert + * @return number + */ +long json_token2num(const char *jstr, jtok_t * __restrict tok); +/* + * json_token2array() - Convert token to string array + * + * @jstr = JSON string + * @tok = Token for convert + * return: =NULL error or !=NULL allocated array with string variables, + * after use should be ait_freeVars() + */ +array_t *json_token2array(const char *jstr, jtok_t * __restrict tok); + +/* + * json_findbykey() - Find data by key + * + * @jstr = JSON string + * @key = Search key + * @toks = Parsed tokens + * @toksnum = Number of parsed tokens + * return: =NULL error or !=NULL data token found + */ +jtok_t *json_findbykey(const char *jstr, const char *key, + jtok_t * __restrict toks, int toksnum); + + +/* + * json_dump_yaml() - Dump parsed JSON string to YAML format + * + * @f = Output handler + * @jstr = JSON string + * @toks = JSON tokens + * @toksnum = Number of tokens + * @indent = Start indent spaces + * return: 0 done and 1 added one more item + */ +int json_dump_yaml(FILE *f, const char *jstr, jtok_t *toks, int toksnum, int indent); + + +/* + * json_add_begin_object() - Adds begin of object { + * + * @jstr = JSON string + * @jlen = JSON string length + * @wspace = whitespace include + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_begin_object(char * __restrict jstr, int jlen, int wspace); +/* + * json_add_end_object() - Adds end of object } + * + * @jstr = JSON string + * @jlen = JSON string length + * @wspace = whitespace include + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_end_object(char * __restrict jstr, int jlen, int wspace); +/* + * json_add_begin_array() - Adds begin of array [ + * + * @jstr = JSON string + * @jlen = JSON string length + * @wspace = whitespace include + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_begin_array(char * __restrict jstr, int jlen, int wspace); +/* + * json_add_end_array() - Adds end of array ] + * + * @jstr = JSON string + * @jlen = JSON string length + * @wspace = whitespace include + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_end_array(char * __restrict jstr, int jlen, int wspace); +/* + * json_add_char() - Adds character + * + * @jstr = JSON string + * @jlen = JSON string length + * @ch = Character + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_char(char * __restrict jstr, int jlen, unsigned char ch); +/* + * json_add_colon() - Adds key/value pair delimiter colon : + * + * @jstr = JSON string + * @jlen = JSON string length + * @wspace = whitespace include + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_colon(char * __restrict jstr, int jlen, int wspace); +/* + * json_add_comma() - Adds value delimiter comma , + * + * @jstr = JSON string + * @jlen = JSON string length + * @wspace = whitespace include + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_comma(char * __restrict jstr, int jlen, int wspace); +/* + * json_add_string() - Adds string + * + * @jstr = JSON string + * @jlen = JSON string length + * @unquot = Unquoted string + * @str = String, it can't be NULL + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_string(char * __restrict jstr, int jlen, int unquot, const char *str); +/* + * json_add_value() - Adds value + * + * @jstr = JSON string + * @jlen = JSON string length + * @unquot = Unquoted number + * @num = Number + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_value(char * __restrict jstr, int jlen, int unquot, long num); +/* + * json_add_pair() - Adds key/value pair + * + * @jstr = JSON string + * @jlen = JSON string length + * @wspace = whitespace include + * @key = Key string + * @val = Value string + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_pair(char * __restrict jstr, int jlen, int wspace, + const char *key, const char *val); +/* + * json_add_array() - Adds array + * + * @jstr = JSON string + * @jlen = JSON string length + * @wspace = whitespace include + * @arr = Array with variables + * return: -1 error or !=-1 actual JSON string length + */ +int json_add_array(char * __restrict jstr, int jlen, int wspace, + array_t * __restrict arr); #endif