version 1.1.2.3, 2017/11/27 19:37:22
|
version 1.1.2.7, 2017/11/28 13:42:22
|
Line 116 json_gettoken(json_t * __restrict json, jtok_t * __res
|
Line 116 json_gettoken(json_t * __restrict json, jtok_t * __res
|
elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]); |
elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]); |
return NULL; |
return NULL; |
} else |
} else |
tok = &jtoks[json->h_next++]; | tok = &jtoks[json->h_next]; |
| tok->tok_idx = json->h_next++; |
tok->tok_start = tok->tok_end = tok->tok_parent = -1; |
tok->tok_start = tok->tok_end = tok->tok_parent = -1; |
tok->tok_size = 0; |
tok->tok_size = 0; |
|
|
Line 276 json_parse(json_t * __restrict json, const char *jstr,
|
Line 277 json_parse(json_t * __restrict json, const char *jstr,
|
switch ((ch = jstr[json->h_pos])) { |
switch ((ch = jstr[json->h_pos])) { |
case '{': |
case '{': |
case '[': |
case '[': |
cx++; /* start new token */ | cx++; /* start new object/array token */ |
if (!jtoks) |
if (!jtoks) |
break; |
break; |
|
|
Line 328 json_parse(json_t * __restrict json, const char *jstr,
|
Line 329 json_parse(json_t * __restrict json, const char *jstr,
|
elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]); |
elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]); |
return (u_int) -1; |
return (u_int) -1; |
} |
} |
cx++; | cx++; /* start new string token */ |
if (jtoks && json->h_parent != -1) |
if (jtoks && json->h_parent != -1) |
jtoks[json->h_parent].tok_size++; |
jtoks[json->h_parent].tok_size++; |
break; |
break; |
Line 374 json_parse(json_t * __restrict json, const char *jstr,
|
Line 375 json_parse(json_t * __restrict json, const char *jstr,
|
|
|
if (json_parse_value(json, jstr, jlen, jtoks, toksnum) == -1) |
if (json_parse_value(json, jstr, jlen, jtoks, toksnum) == -1) |
return (u_int) -1; |
return (u_int) -1; |
cx++; | cx++; /* start new value token */ |
if (jtoks && json->h_parent != -1) |
if (jtoks && json->h_parent != -1) |
jtoks[json->h_parent].tok_size++; |
jtoks[json->h_parent].tok_size++; |
break; |
break; |
Line 387 json_parse(json_t * __restrict json, const char *jstr,
|
Line 388 json_parse(json_t * __restrict json, const char *jstr,
|
|
|
if (json_parse_value(json, jstr, jlen, jtoks, toksnum) == -1) |
if (json_parse_value(json, jstr, jlen, jtoks, toksnum) == -1) |
return (u_int) -1; |
return (u_int) -1; |
cx++; | cx++; /* start new value token */ |
if (jtoks && json->h_parent != -1) |
if (jtoks && json->h_parent != -1) |
jtoks[json->h_parent].tok_size++; |
jtoks[json->h_parent].tok_size++; |
break; |
break; |
Line 402 json_parse(json_t * __restrict json, const char *jstr,
|
Line 403 json_parse(json_t * __restrict json, const char *jstr,
|
return (u_int) -1; |
return (u_int) -1; |
} |
} |
} |
} |
} | } else |
| cx++; /* increment needed tokens number for termination empty token */ |
|
|
return cx; |
return cx; |
|
} |
|
|
|
/* |
|
* json_token2val() - Return token to AIT variable |
|
* |
|
* @jstr = JSON string |
|
* @tok = Token for convert |
|
* @return =NULL error or !=NULL allocated variable, after use should be ait_freeVar() |
|
*/ |
|
ait_val_t * |
|
json_token2val(const char *jstr, jtok_t * __restrict tok) |
|
{ |
|
ait_val_t *v = NULL; |
|
|
|
if (!jstr || !tok) |
|
return NULL; |
|
|
|
v = ait_allocVar(); |
|
if (!v) |
|
return NULL; |
|
|
|
AIT_SET_STRSIZ(v, json_toklen(tok)); |
|
strncpy(AIT_GET_STR(v), json_tokstr(jstr, tok), AIT_LEN(v) - 1); |
|
|
|
return v; |
|
} |
|
|
|
/* |
|
* 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) |
|
{ |
|
char *str = NULL; |
|
size_t len; |
|
|
|
if (!jstr || !tok) |
|
return NULL; |
|
|
|
len = json_toklen(tok); |
|
str = e_malloc(len + 1); |
|
if (!str) |
|
return NULL; |
|
else { |
|
strncpy(str, json_tokstr(jstr, tok), len); |
|
str[len] = 0; |
|
} |
|
|
|
return str; |
|
} |
|
|
|
/* |
|
* 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) |
|
{ |
|
long ret = 0; |
|
char *str; |
|
|
|
str = json_token2str(jstr, tok); |
|
if (!str) |
|
return 0; |
|
|
|
ret = strtol(str, NULL, 0); |
|
e_free(str); |
|
return ret; |
|
} |
|
|
|
/* |
|
* 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) |
|
{ |
|
jtok_t *tok = NULL; |
|
register int i; |
|
int klen; |
|
|
|
if (!jstr || !key || !toks) |
|
return NULL; |
|
else |
|
klen = strlen(key); |
|
|
|
for (i = 1; i < toksnum; i++) { |
|
if (toks[i].tok_type == J_STRING && |
|
klen == toks[i].tok_end - toks[i].tok_start && |
|
!strncmp(jstr + toks[i].tok_start, key, klen)) { |
|
tok = toks + i + 1; |
|
break; |
|
} |
|
} |
|
|
|
return 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) |
|
{ |
|
array_t *arr = NULL; |
|
register int i; |
|
int siz; |
|
ait_val_t *v; |
|
jtok_t *t; |
|
|
|
if (!jstr || !tok) |
|
return NULL; |
|
|
|
siz = tok->tok_size; |
|
if (!siz && json_toktype(tok) != J_ARRAY && json_toktype(tok) != J_OBJECT) |
|
siz++; |
|
|
|
arr = ait_allocVars(siz); |
|
if (!arr) |
|
return NULL; |
|
|
|
if (tok->tok_type == J_STRING || tok->tok_type == J_VALUE) { |
|
v = ait_getVars(&arr, 0); |
|
AIT_SET_STRSIZ(v, json_toklen(tok) + 1); |
|
json_tokstrcpy(AIT_GET_STR(v), jstr, tok); |
|
} else if (tok->tok_type == J_ARRAY) { |
|
for (i = 0; i < tok->tok_size; i++) { |
|
t = &tok[i + 1]; |
|
v = ait_getVars(&arr, i); |
|
AIT_SET_STRSIZ(v, json_toklen(t) + 1); |
|
json_tokstrcpy(AIT_GET_STR(v), jstr, t); |
|
} |
|
} else if (tok->tok_type == J_OBJECT) { |
|
for (i = 0; tok->tok_idx <= tok[i + 1].tok_parent; i++) { |
|
t = &tok[i + 1]; |
|
v = ait_getVars(&arr, i); |
|
AIT_SET_STRSIZ(v, json_toklen(t) + 1); |
|
json_tokstrcpy(AIT_GET_STR(v), jstr, t); |
|
} |
|
} else { |
|
elwix_SetErr(J_ERR_PARAM, "%s", jerrstr[J_ERR_PARAM]); |
|
return NULL; |
|
} |
|
|
|
return arr; |
} |
} |