version 1.1.2.3, 2017/11/27 19:37:22
|
version 1.1.2.4, 2017/11/28 00:46:53
|
Line 406 json_parse(json_t * __restrict json, const char *jstr,
|
Line 406 json_parse(json_t * __restrict json, const char *jstr,
|
|
|
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, tok->tok_end - tok->tok_start); |
|
strncpy(AIT_GET_STR(v), jstr + tok->tok_start, 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 = tok->tok_end - tok->tok_start; |
|
str = e_malloc(len + 1); |
|
if (!str) |
|
return NULL; |
|
else { |
|
strncpy(str, jstr + tok->tok_start, 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 array |
|
* |
|
* @jstr = JSON string |
|
* @tok = Token for convert |
|
* return: =NULL error or !=NULL allocated array with 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 && tok->tok_type != J_ARRAY && tok->tok_type != 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, tok->tok_end - tok->tok_start); |
|
strncpy(AIT_GET_STR(v), jstr + tok->tok_start, AIT_LEN(v) - 1); |
|
} 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, t->tok_end - t->tok_start); |
|
strncpy(AIT_GET_STR(v), jstr + t->tok_start, AIT_LEN(v) - 1); |
|
} |
|
} else { |
|
/* todo for object */ |
|
} |
|
|
|
return arr; |
|
} |