snac2/xs_json.h

549 lines
11 KiB
C
Raw Permalink Normal View History

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