snac2/xs_json.h

550 lines
11 KiB
C
Raw Normal View History

2024-01-04 11:22:03 +03:00
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
2022-09-19 21:41:11 +03:00
#ifndef _XS_JSON_H
#define _XS_JSON_H
int xs_json_dump(const xs_val *data, int indent, FILE *f);
xs_str *xs_json_dumps(const xs_val *data, int indent);
2023-09-03 21:22:32 +03:00
xs_val *xs_json_load(FILE *f);
2023-09-03 21:22:32 +03:00
xs_val *xs_json_loads(const xs_str *json);
xstype xs_json_load_type(FILE *f);
2024-03-25 10:55:30 +03:00
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, xstype *pt, int *c);
xs_list *xs_json_load_array(FILE *f);
xs_dict *xs_json_load_object(FILE *f);
2022-09-19 21:41:11 +03:00
#ifdef XS_IMPLEMENTATION
/** IMPLEMENTATION **/
/** JSON dumps **/
2023-08-05 22:57:45 +03:00
static void _xs_json_dump_str(const char *data, FILE *f)
2022-09-19 21:41:11 +03:00
/* dumps a string in JSON format */
{
unsigned char c;
2023-08-05 22:57:45 +03:00
fputs("\"", f);
2022-09-19 21:41:11 +03:00
while ((c = *data)) {
if (c == '\n')
2023-08-05 22:57:45 +03:00
fputs("\\n", f);
2022-09-19 21:41:11 +03:00
else
if (c == '\r')
2023-08-05 22:57:45 +03:00
fputs("\\r", f);
2022-09-19 21:41:11 +03:00
else
if (c == '\t')
2023-08-05 22:57:45 +03:00
fputs("\\t", f);
2022-09-19 21:41:11 +03:00
else
if (c == '\\')
2023-08-05 22:57:45 +03:00
fputs("\\\\", f);
2022-09-19 21:41:11 +03:00
else
if (c == '"')
2023-08-05 22:57:45 +03:00
fputs("\\\"", f);
2022-09-19 21:41:11 +03:00
else
2023-08-05 22:57:45 +03:00
if (c < 32)
fprintf(f, "\\u%04x", (unsigned int) c);
2022-09-19 21:41:11 +03:00
else
2023-08-05 22:57:45 +03:00
fputc(c, f);
2022-09-19 21:41:11 +03:00
data++;
}
2023-08-05 22:57:45 +03:00
fputs("\"", f);
2022-09-19 21:41:11 +03:00
}
2023-08-05 22:57:45 +03:00
static void _xs_json_indent(int level, int indent, FILE *f)
2022-09-19 21:41:11 +03:00
/* adds indentation */
{
if (indent) {
int n;
2023-08-05 22:57:45 +03:00
fputc('\n', f);
2022-09-19 21:41:11 +03:00
for (n = 0; n < level * indent; n++)
2023-08-05 22:57:45 +03:00
fputc(' ', f);
2022-09-19 21:41:11 +03:00
}
}
2024-05-21 15:12:15 +03:00
static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
2022-09-19 21:41:11 +03:00
/* dumps partial data as JSON */
{
int c = 0;
2024-05-21 15:12:15 +03:00
int ct = 0;
2024-05-23 11:01:37 +03:00
const xs_val *v;
2022-09-19 21:41:11 +03:00
switch (xs_type(data)) {
case XSTYPE_NULL:
2023-08-05 22:57:45 +03:00
fputs("null", f);
2022-09-19 21:41:11 +03:00
break;
case XSTYPE_TRUE:
2023-08-05 22:57:45 +03:00
fputs("true", f);
2022-09-19 21:41:11 +03:00
break;
case XSTYPE_FALSE:
2023-08-05 22:57:45 +03:00
fputs("false", f);
2022-09-19 21:41:11 +03:00
break;
case XSTYPE_NUMBER:
2023-08-05 22:57:45 +03:00
fputs(xs_number_str(data), f);
2022-09-19 21:41:11 +03:00
break;
2022-09-25 23:57:18 +03:00
case XSTYPE_LIST:
2023-08-05 22:57:45 +03:00
fputc('[', f);
2022-09-19 21:41:11 +03:00
2024-05-21 15:12:15 +03:00
while (xs_list_next(data, &v, &ct)) {
2022-09-19 21:41:11 +03:00
if (c != 0)
2023-08-05 22:57:45 +03:00
fputc(',', f);
2022-09-19 21:41:11 +03:00
2023-08-05 22:57:45 +03:00
_xs_json_indent(level + 1, indent, f);
_xs_json_dump(v, level + 1, indent, f);
2022-09-19 21:41:11 +03:00
c++;
}
2023-08-05 22:57:45 +03:00
_xs_json_indent(level, indent, f);
fputc(']', f);
2022-09-19 21:41:11 +03:00
break;
2022-09-25 23:57:18 +03:00
case XSTYPE_DICT:
2023-08-05 22:57:45 +03:00
fputc('{', f);
2022-09-19 21:41:11 +03:00
2024-05-23 11:01:37 +03:00
const xs_str *k;
2024-03-09 10:32:20 +03:00
2024-05-21 15:12:15 +03:00
while (xs_dict_next(data, &k, &v, &ct)) {
2022-09-19 21:41:11 +03:00
if (c != 0)
2023-08-05 22:57:45 +03:00
fputc(',', f);
2022-09-19 21:41:11 +03:00
2023-08-05 22:57:45 +03:00
_xs_json_indent(level + 1, indent, f);
2022-09-19 21:41:11 +03:00
2023-08-05 22:57:45 +03:00
_xs_json_dump_str(k, f);
fputc(':', f);
2022-09-19 21:41:11 +03:00
if (indent)
2023-08-05 22:57:45 +03:00
fputc(' ', f);
2022-09-19 21:41:11 +03:00
2023-08-05 22:57:45 +03:00
_xs_json_dump(v, level + 1, indent, f);
2022-09-19 21:41:11 +03:00
c++;
}
2023-08-05 22:57:45 +03:00
_xs_json_indent(level, indent, f);
fputc('}', f);
2022-09-19 21:41:11 +03:00
break;
case XSTYPE_STRING:
2023-08-05 22:57:45 +03:00
_xs_json_dump_str(data, f);
2022-09-19 21:41:11 +03:00
break;
default:
break;
}
}
xs_str *xs_json_dumps(const xs_val *data, int indent)
2023-08-05 22:57:45 +03:00
/* dumps data as a JSON string */
2022-09-19 21:41:11 +03:00
{
2023-01-28 19:49:02 +03:00
xs_str *s = NULL;
2023-08-05 22:57:45 +03:00
size_t sz;
FILE *f;
2022-09-19 21:41:11 +03:00
2023-08-05 22:57:45 +03:00
if ((f = open_memstream(&s, &sz)) != NULL) {
int r = xs_json_dump(data, indent, f);
2023-08-05 22:57:45 +03:00
fclose(f);
if (!r)
s = xs_free(s);
2022-09-19 21:41:11 +03:00
}
return s;
}
int xs_json_dump(const xs_val *data, int indent, FILE *f)
2023-08-03 09:42:38 +03:00
/* dumps data into a file as JSON */
{
2023-08-05 22:57:45 +03:00
xstype t = xs_type(data);
2023-08-03 09:42:38 +03:00
2023-08-05 22:57:45 +03:00
if (t == XSTYPE_LIST || t == XSTYPE_DICT) {
_xs_json_dump(data, 0, indent, f);
return 1;
}
2023-08-03 09:42:38 +03:00
2023-08-05 22:57:45 +03:00
return 0;
2023-08-03 09:42:38 +03:00
}
2022-09-19 21:41:11 +03:00
/** JSON loads **/
typedef enum {
JS_ERROR = -1,
JS_INCOMPLETE,
JS_OCURLY,
JS_OBRACK,
JS_CCURLY,
JS_CBRACK,
JS_COMMA,
JS_COLON,
JS_VALUE,
JS_STRING,
2023-08-09 16:54:36 +03:00
JS_NUMBER,
2022-09-19 21:41:11 +03:00
JS_TRUE,
JS_FALSE,
JS_NULL,
JS_ARRAY,
JS_OBJECT
} js_type;
2023-08-08 14:04:55 +03:00
static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
2022-09-19 21:41:11 +03:00
{
2023-08-08 14:04:55 +03:00
int c;
2023-02-28 13:22:52 +03:00
xs_val *v = NULL;
2024-08-23 18:22:10 +03:00
int offset;
2022-09-19 21:41:11 +03:00
2023-08-08 14:04:55 +03:00
*t = JS_ERROR;
2022-09-19 21:41:11 +03:00
2023-08-08 14:04:55 +03:00
/* skip blanks */
while ((c = fgetc(f)) == L' ' || c == L'\t' || c == L'\n' || c == L'\r');
2022-09-19 21:41:11 +03:00
if (c == '{')
*t = JS_OCURLY;
else
if (c == '}')
*t = JS_CCURLY;
else
if (c == '[')
*t = JS_OBRACK;
else
if (c == ']')
*t = JS_CBRACK;
else
if (c == ',')
*t = JS_COMMA;
else
if (c == ':')
*t = JS_COLON;
else
if (c == '"') {
*t = JS_STRING;
v = xs_str_new(NULL);
2024-08-23 18:22:10 +03:00
offset = 0;
2022-09-19 21:41:11 +03:00
2023-08-08 14:04:55 +03:00
while ((c = fgetc(f)) != '"' && c != EOF && *t != JS_ERROR) {
2022-09-19 21:41:11 +03:00
if (c == '\\') {
2023-08-08 14:04:55 +03:00
unsigned int cp = fgetc(f);
2022-09-19 21:41:11 +03:00
2023-08-08 14:04:55 +03:00
switch (cp) {
case 'n': cp = '\n'; break;
case 'r': cp = '\r'; break;
case 't': cp = '\t'; break;
case 'u': /* Unicode codepoint as an hex char */
if (fscanf(f, "%04x", &cp) != 1) {
2023-05-15 11:31:15 +03:00
*t = JS_ERROR;
break;
}
2023-11-17 05:51:04 +03:00
if (xs_is_surrogate(cp)) {
2023-08-08 14:04:55 +03:00
/* \u must follow */
if (fgetc(f) != '\\' || fgetc(f) != 'u') {
2023-05-15 11:31:15 +03:00
*t = JS_ERROR;
break;
}
2023-11-17 05:51:04 +03:00
unsigned int p2;
if (fscanf(f, "%04x", &p2) != 1) {
2023-05-15 11:31:15 +03:00
*t = JS_ERROR;
break;
}
2023-11-17 05:51:04 +03:00
cp = xs_surrogate_dec(cp, p2);
}
/* replace dangerous control codes with their visual representations */
2023-08-08 14:04:55 +03:00
if (cp < ' ' && !strchr("\r\n\t", cp))
cp += 0x2400;
2023-03-07 11:56:16 +03:00
2022-09-19 21:41:11 +03:00
break;
}
2024-08-23 18:22:10 +03:00
v = xs_utf8_insert(v, cp, &offset);
2023-08-08 14:04:55 +03:00
}
else {
char cc = c;
2024-08-23 18:22:10 +03:00
v = xs_insert_m(v, offset, &cc, 1);
offset++;
2023-08-08 14:04:55 +03:00
}
2022-09-19 21:41:11 +03:00
}
2023-08-12 09:37:31 +03:00
if (c == EOF)
*t = JS_ERROR;
2022-09-19 21:41:11 +03:00
}
else
if (c == '-' || (c >= '0' && c <= '9') || c == '.') {
2023-08-08 14:04:55 +03:00
double d;
2022-09-19 21:41:11 +03:00
2023-08-08 14:04:55 +03:00
ungetc(c, f);
if (fscanf(f, "%lf", &d) == 1) {
2023-08-09 16:54:36 +03:00
*t = JS_NUMBER;
2023-08-08 14:04:55 +03:00
v = xs_number_new(d);
2022-09-19 21:41:11 +03:00
}
}
else
2023-08-08 14:04:55 +03:00
if (c == 't') {
if (fgetc(f) == 'r' && fgetc(f) == 'u' && fgetc(f) == 'e') {
*t = JS_TRUE;
v = xs_val_new(XSTYPE_TRUE);
}
2022-09-19 21:41:11 +03:00
}
else
2023-08-08 14:04:55 +03:00
if (c == 'f') {
if (fgetc(f) == 'a' && fgetc(f) == 'l' &&
fgetc(f) == 's' && fgetc(f) == 'e') {
*t = JS_FALSE;
v = xs_val_new(XSTYPE_FALSE);
}
2022-09-19 21:41:11 +03:00
}
else
2023-08-08 14:04:55 +03:00
if (c == 'n') {
if (fgetc(f) == 'u' && fgetc(f) == 'l' && fgetc(f) == 'l') {
*t = JS_NULL;
v = xs_val_new(XSTYPE_NULL);
}
2022-09-19 21:41:11 +03:00
}
2023-08-12 09:37:31 +03:00
if (*t == JS_ERROR)
v = xs_free(v);
2022-09-19 21:41:11 +03:00
return v;
}
2024-03-25 10:55:30 +03:00
int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
/* loads the next scalar value from the JSON stream */
2024-04-14 20:26:49 +03:00
/* if the value ahead is compound, value is NULL and pt is set */
2023-09-03 21:22:32 +03:00
{
js_type t;
*value = _xs_json_load_lexer(f, &t);
if (t == JS_ERROR)
return -1;
if (t == JS_CBRACK)
return 0;
if (*c > 0) {
if (t == JS_COMMA)
*value = _xs_json_load_lexer(f, &t);
else
return -1;
}
2024-03-25 10:55:30 +03:00
if (*value == NULL) {
2024-04-14 20:26:49 +03:00
/* possible compound type ahead */
2024-03-25 10:55:30 +03:00
if (t == JS_OBRACK)
*pt = XSTYPE_LIST;
else
if (t == JS_OCURLY)
*pt = XSTYPE_DICT;
else
return -1;
}
2023-09-03 21:22:32 +03:00
*c = *c + 1;
return 1;
}
2024-03-25 10:55:30 +03:00
xs_list *xs_json_load_array(FILE *f)
2024-04-14 20:26:49 +03:00
/* loads a full JSON array (after the initial OBRACK) */
2022-09-19 21:41:11 +03:00
{
2024-03-25 10:55:30 +03:00
xstype t;
2023-08-12 09:37:31 +03:00
xs_list *l = xs_list_new();
int c = 0;
2022-09-19 21:41:11 +03:00
2023-08-21 10:32:05 +03:00
for (;;) {
2023-09-03 21:22:32 +03:00
xs *v = NULL;
2024-03-25 10:55:30 +03:00
int r = xs_json_load_array_iter(f, &v, &t, &c);
2023-09-03 21:22:32 +03:00
if (r == -1)
l = xs_free(l);
2022-09-19 21:41:11 +03:00
2024-03-25 10:55:30 +03:00
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;
}
2023-09-03 21:22:32 +03:00
l = xs_list_append(l, v);
2024-03-25 10:55:30 +03:00
}
2023-09-03 21:22:32 +03:00
else
2023-08-12 09:37:31 +03:00
break;
2023-09-03 21:22:32 +03:00
}
2022-09-19 21:41:11 +03:00
2023-09-03 21:22:32 +03:00
return l;
}
2022-09-19 21:41:11 +03:00
2024-03-25 10:55:30 +03:00
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 */
2024-04-14 20:26:49 +03:00
/* if the value ahead is compound, value is NULL and pt is set */
2023-09-03 21:22:32 +03:00
{
js_type t;
2022-09-19 21:41:11 +03:00
2023-09-03 21:22:32 +03:00
*key = _xs_json_load_lexer(f, &t);
if (t == JS_ERROR)
return -1;
2022-09-19 21:41:11 +03:00
2023-09-03 21:22:32 +03:00
if (t == JS_CCURLY)
return 0;
if (*c > 0) {
if (t == JS_COMMA)
*key = _xs_json_load_lexer(f, &t);
else
return -1;
2022-09-19 21:41:11 +03:00
}
2023-09-03 21:22:32 +03:00
if (t != JS_STRING)
return -1;
xs_free(_xs_json_load_lexer(f, &t));
if (t != JS_COLON)
return -1;
*value = _xs_json_load_lexer(f, &t);
2024-03-25 10:55:30 +03:00
if (*value == NULL) {
/* possible complex type ahead */
if (t == JS_OBRACK)
*pt = XSTYPE_LIST;
else
if (t == JS_OCURLY)
*pt = XSTYPE_DICT;
else
return -1;
}
2023-09-03 21:22:32 +03:00
*c = *c + 1;
return 1;
2022-09-19 21:41:11 +03:00
}
2024-03-25 10:55:30 +03:00
xs_dict *xs_json_load_object(FILE *f)
2024-04-14 20:26:49 +03:00
/* loads a full JSON object (after the initial OCURLY) */
2022-09-19 21:41:11 +03:00
{
2024-03-25 10:55:30 +03:00
xstype t;
2023-08-12 09:37:31 +03:00
xs_dict *d = xs_dict_new();
int c = 0;
2022-09-19 21:41:11 +03:00
2023-08-21 10:32:05 +03:00
for (;;) {
2023-09-03 21:22:32 +03:00
xs *k = NULL;
2023-08-12 09:37:31 +03:00
xs *v = NULL;
2024-03-25 10:55:30 +03:00
int r = xs_json_load_object_iter(f, &k, &v, &t, &c);
2022-09-19 21:41:11 +03:00
2023-09-03 21:22:32 +03:00
if (r == -1)
d = xs_free(d);
2022-09-19 21:41:11 +03:00
2024-03-25 10:55:30 +03:00
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;
}
2023-09-03 21:22:32 +03:00
d = xs_dict_append(d, k, v);
2024-03-25 10:55:30 +03:00
}
2023-08-12 09:37:31 +03:00
else
break;
2022-09-19 21:41:11 +03:00
}
2023-09-03 21:22:32 +03:00
return d;
2022-09-19 21:41:11 +03:00
}
2023-01-28 19:49:02 +03:00
xs_val *xs_json_loads(const xs_str *json)
2022-09-19 21:41:11 +03:00
/* loads a string in JSON format and converts to a multiple data */
2023-08-08 14:04:55 +03:00
{
FILE *f;
xs_val *v = NULL;
if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) {
v = xs_json_load(f);
fclose(f);
}
return v;
}
2023-09-03 21:22:32 +03:00
xstype xs_json_load_type(FILE *f)
/* identifies the type of a JSON stream */
{
xstype t = XSTYPE_NULL;
js_type jt;
xs_free(_xs_json_load_lexer(f, &jt));
if (jt == JS_OBRACK)
t = XSTYPE_LIST;
else
if (jt == JS_OCURLY)
t = XSTYPE_DICT;
return t;
}
2023-08-08 14:04:55 +03:00
xs_val *xs_json_load(FILE *f)
/* loads a JSON file */
2022-09-19 21:41:11 +03:00
{
2023-01-28 19:49:02 +03:00
xs_val *v = NULL;
2023-09-03 21:22:32 +03:00
xstype t = xs_json_load_type(f);
2022-09-19 21:41:11 +03:00
2023-09-03 21:22:32 +03:00
if (t == XSTYPE_LIST)
2024-03-25 10:55:30 +03:00
v = xs_json_load_array(f);
2022-09-19 21:41:11 +03:00
else
2023-09-03 21:22:32 +03:00
if (t == XSTYPE_DICT)
2024-03-25 10:55:30 +03:00
v = xs_json_load_object(f);
2022-09-19 21:41:11 +03:00
return v;
}
2022-09-19 21:41:11 +03:00
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_JSON_H */