mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-25 14:35:04 +00:00
Backport from xs.
This commit is contained in:
parent
8ff9d84bd8
commit
abf4279000
1 changed files with 74 additions and 33 deletions
107
xs_json.h
107
xs_json.h
|
@ -11,8 +11,10 @@ xs_val *xs_json_load(FILE *f);
|
||||||
xs_val *xs_json_loads(const xs_str *json);
|
xs_val *xs_json_loads(const xs_str *json);
|
||||||
|
|
||||||
xstype xs_json_load_type(FILE *f);
|
xstype xs_json_load_type(FILE *f);
|
||||||
int xs_json_load_array_iter(FILE *f, xs_val **value, int *c);
|
int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c);
|
||||||
int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, int *c);
|
int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c);
|
||||||
|
xs_list *xs_json_load_array(FILE *f);
|
||||||
|
xs_dict *xs_json_load_object(FILE *f);
|
||||||
|
|
||||||
|
|
||||||
#ifdef XS_IMPLEMENTATION
|
#ifdef XS_IMPLEMENTATION
|
||||||
|
@ -324,10 +326,9 @@ static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static xs_list *_xs_json_load_array(FILE *f);
|
int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
|
||||||
static xs_dict *_xs_json_load_object(FILE *f);
|
/* loads the next scalar value from the JSON stream */
|
||||||
|
/* if the value ahead is complex, value is NULL and pt is filled */
|
||||||
int xs_json_load_array_iter(FILE *f, xs_val **value, int *c)
|
|
||||||
{
|
{
|
||||||
js_type t;
|
js_type t;
|
||||||
|
|
||||||
|
@ -346,14 +347,16 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, int *c)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (t == JS_OBRACK)
|
if (*value == NULL) {
|
||||||
*value = _xs_json_load_array(f);
|
/* possible complex type ahead */
|
||||||
else
|
if (t == JS_OBRACK)
|
||||||
if (t == JS_OCURLY)
|
*pt = XSTYPE_LIST;
|
||||||
*value = _xs_json_load_object(f);
|
else
|
||||||
|
if (t == JS_OCURLY)
|
||||||
if (*value == NULL)
|
*pt = XSTYPE_DICT;
|
||||||
return -1;
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
*c = *c + 1;
|
*c = *c + 1;
|
||||||
|
|
||||||
|
@ -361,21 +364,38 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, int *c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static xs_list *_xs_json_load_array(FILE *f)
|
xs_list *xs_json_load_array(FILE *f)
|
||||||
/* parses a JSON array */
|
/* loads a JSON array (after the initial OBRACK) */
|
||||||
{
|
{
|
||||||
|
xstype t;
|
||||||
xs_list *l = xs_list_new();
|
xs_list *l = xs_list_new();
|
||||||
int c = 0;
|
int c = 0;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
xs *v = NULL;
|
xs *v = NULL;
|
||||||
int r = xs_json_load_array_iter(f, &v, &c);
|
int r = xs_json_load_array_iter(f, &v, &t, &c);
|
||||||
|
|
||||||
if (r == -1)
|
if (r == -1)
|
||||||
l = xs_free(l);
|
l = xs_free(l);
|
||||||
|
|
||||||
if (r == 1)
|
if (r == 1) {
|
||||||
|
/* partial load? */
|
||||||
|
if (v == NULL) {
|
||||||
|
if (t == XSTYPE_LIST)
|
||||||
|
v = xs_json_load_array(f);
|
||||||
|
else
|
||||||
|
if (t == XSTYPE_DICT)
|
||||||
|
v = xs_json_load_object(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* still null? fail */
|
||||||
|
if (v == NULL) {
|
||||||
|
l = xs_free(l);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
l = xs_list_append(l, v);
|
l = xs_list_append(l, v);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -384,7 +404,9 @@ static xs_list *_xs_json_load_array(FILE *f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, int *c)
|
int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c)
|
||||||
|
/* loads the next key and scalar value from the JSON stream */
|
||||||
|
/* if the value ahead is complex, value is NULL and pt is filled */
|
||||||
{
|
{
|
||||||
js_type t;
|
js_type t;
|
||||||
|
|
||||||
|
@ -413,14 +435,16 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, int *c)
|
||||||
|
|
||||||
*value = _xs_json_load_lexer(f, &t);
|
*value = _xs_json_load_lexer(f, &t);
|
||||||
|
|
||||||
if (t == JS_OBRACK)
|
if (*value == NULL) {
|
||||||
*value = _xs_json_load_array(f);
|
/* possible complex type ahead */
|
||||||
else
|
if (t == JS_OBRACK)
|
||||||
if (t == JS_OCURLY)
|
*pt = XSTYPE_LIST;
|
||||||
*value = _xs_json_load_object(f);
|
else
|
||||||
|
if (t == JS_OCURLY)
|
||||||
if (*value == NULL)
|
*pt = XSTYPE_DICT;
|
||||||
return -1;
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
*c = *c + 1;
|
*c = *c + 1;
|
||||||
|
|
||||||
|
@ -428,22 +452,39 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, int *c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static xs_dict *_xs_json_load_object(FILE *f)
|
xs_dict *xs_json_load_object(FILE *f)
|
||||||
/* parses a JSON object */
|
/* loads a JSON object (after the initial OCURLY) */
|
||||||
{
|
{
|
||||||
|
xstype t;
|
||||||
xs_dict *d = xs_dict_new();
|
xs_dict *d = xs_dict_new();
|
||||||
int c = 0;
|
int c = 0;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
xs *k = NULL;
|
xs *k = NULL;
|
||||||
xs *v = NULL;
|
xs *v = NULL;
|
||||||
int r = xs_json_load_object_iter(f, &k, &v, &c);
|
int r = xs_json_load_object_iter(f, &k, &v, &t, &c);
|
||||||
|
|
||||||
if (r == -1)
|
if (r == -1)
|
||||||
d = xs_free(d);
|
d = xs_free(d);
|
||||||
|
|
||||||
if (r == 1)
|
if (r == 1) {
|
||||||
|
/* partial load? */
|
||||||
|
if (v == NULL) {
|
||||||
|
if (t == XSTYPE_LIST)
|
||||||
|
v = xs_json_load_array(f);
|
||||||
|
else
|
||||||
|
if (t == XSTYPE_DICT)
|
||||||
|
v = xs_json_load_object(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* still null? fail */
|
||||||
|
if (v == NULL) {
|
||||||
|
d = xs_free(d);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
d = xs_dict_append(d, k, v);
|
d = xs_dict_append(d, k, v);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -492,10 +533,10 @@ xs_val *xs_json_load(FILE *f)
|
||||||
xstype t = xs_json_load_type(f);
|
xstype t = xs_json_load_type(f);
|
||||||
|
|
||||||
if (t == XSTYPE_LIST)
|
if (t == XSTYPE_LIST)
|
||||||
v = _xs_json_load_array(f);
|
v = xs_json_load_array(f);
|
||||||
else
|
else
|
||||||
if (t == XSTYPE_DICT)
|
if (t == XSTYPE_DICT)
|
||||||
v = _xs_json_load_object(f);
|
v = xs_json_load_object(f);
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue