snac2/xs_json.h

551 lines
12 KiB
C
Raw Permalink Normal View History

2023-07-28 09:34:18 +00:00
/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
2022-09-19 18:41:11 +00:00
#ifndef _XS_JSON_H
#define _XS_JSON_H
2023-08-03 06:42:38 +00:00
int xs_json_dump_pp(const xs_val *data, int indent, FILE *f);
2023-08-05 19:57:45 +00:00
xs_str *xs_json_dumps_pp(const xs_val *data, int indent);
2022-09-19 18:41:11 +00:00
#define xs_json_dumps(data) xs_json_dumps_pp(data, 0)
2023-08-05 19:57:45 +00:00
#define xs_json_dump(data, f) xs_json_dumps_pp(data, 0, f)
2023-01-28 16:49:02 +00:00
xs_val *xs_json_loads(const xs_str *json);
xs_val *xs_json_load(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
}
}
2023-08-05 19:57:45 +00:00
static void _xs_json_dump(const xs_val *s_data, int level, int indent, FILE *f)
2022-09-19 18:41:11 +00:00
/* dumps partial data as JSON */
{
int c = 0;
2023-02-28 10:22:52 +00:00
xs_val *v;
xs_val *data = (xs_val *)s_data;
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
while (xs_list_iter(&data, &v)) {
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
2023-02-28 10:22:52 +00:00
xs_str *k;
2022-09-19 18:41:11 +00:00
while (xs_dict_iter(&data, &k, &v)) {
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;
}
}
2023-02-28 10:22:52 +00:00
xs_str *xs_json_dumps_pp(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_pp(data, indent, f);
fclose(f);
if (!r)
s = xs_free(s);
2022-09-19 18:41:11 +00:00
}
return s;
}
2023-08-03 06:42:38 +00:00
int xs_json_dump_pp(const xs_val *data, int indent, FILE *f)
/* 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 **/
/* this code comes mostly from the Minimum Profit Text Editor (MPDM) */
typedef enum {
JS_ERROR = -1,
JS_INCOMPLETE,
JS_OCURLY,
JS_OBRACK,
JS_CCURLY,
JS_CBRACK,
JS_COMMA,
JS_COLON,
JS_VALUE,
JS_STRING,
JS_INTEGER,
JS_REAL,
JS_TRUE,
JS_FALSE,
JS_NULL,
JS_ARRAY,
JS_OBJECT
} js_type;
2023-02-28 10:22:52 +00:00
static xs_val *_xs_json_loads_lexer(const char **json, js_type *t)
2022-09-19 18:41:11 +00:00
{
char c;
const char *s = *json;
2023-02-28 10:22:52 +00:00
xs_val *v = NULL;
2022-09-19 18:41:11 +00:00
/* skip blanks */
while (*s == L' ' || *s == L'\t' || *s == L'\n' || *s == L'\r')
s++;
c = *s++;
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);
while ((c = *s) != '"' && c != '\0') {
char tmp[5];
int cp, i;
2022-09-19 18:41:11 +00:00
if (c == '\\') {
s++;
c = *s;
switch (c) {
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'u': /* Unicode codepoint as an hex char */
s++;
2023-05-15 08:31:15 +00:00
strncpy(tmp, s, 4);
2022-09-19 18:41:11 +00:00
tmp[4] = '\0';
2023-05-15 08:31:15 +00:00
if (strlen(tmp) != 4) {
*t = JS_ERROR;
break;
}
s += 3; /* skip as it was one byte */
2022-09-19 18:41:11 +00:00
sscanf(tmp, "%04x", &i);
if (i >= 0xd800 && i <= 0xdfff) {
/* it's a surrogate pair */
cp = (i & 0x3ff) << 10;
2023-05-15 08:31:15 +00:00
/* skip to the next value (last char + \ + u) */
s++;
if (memcmp(s, "\\u", 2) != 0) {
*t = JS_ERROR;
break;
}
s += 2;
strncpy(tmp, s, 4);
tmp[4] = '\0';
if (strlen(tmp) != 4) {
*t = JS_ERROR;
break;
}
s += 3; /* skip as it was one byte */
sscanf(tmp, "%04x", &i);
cp |= (i & 0x3ff);
cp += 0x10000;
}
else
cp = i;
/* replace dangerous control codes with their visual representations */
2023-03-07 08:56:16 +00:00
if (cp >= '\0' && cp < ' ' && !strchr("\r\n\t", cp))
cp += 0x2400;
2023-03-07 08:56:16 +00:00
v = xs_utf8_enc(v, cp);
2022-09-19 18:41:11 +00:00
c = '\0';
break;
}
}
if (c)
v = xs_append_m(v, &c, 1);
s++;
}
if (c != '\0')
s++;
}
else
if (c == '-' || (c >= '0' && c <= '9') || c == '.') {
xs *vn = NULL;
*t = JS_INTEGER;
vn = xs_str_new(NULL);
vn = xs_append_m(vn, &c, 1);
while (((c = *s) >= '0' && c <= '9') || c == '.') {
if (c == '.')
*t = JS_REAL;
vn = xs_append_m(vn, &c, 1);
s++;
}
/* convert to XSTYPE_NUMBER */
v = xs_number_new(atof(vn));
}
else
if (c == 't' && strncmp(s, "rue", 3) == 0) {
s += 3;
*t = JS_TRUE;
v = xs_val_new(XSTYPE_TRUE);
}
else
if (c == 'f' && strncmp(s, "alse", 4) == 0) {
s += 4;
*t = JS_FALSE;
v = xs_val_new(XSTYPE_FALSE);
}
else
if (c == 'n' && strncmp(s, "ull", 3) == 0) {
s += 3;
*t = JS_NULL;
v = xs_val_new(XSTYPE_NULL);
}
else
*t = JS_ERROR;
*json = s;
return v;
}
2023-02-28 10:22:52 +00:00
static xs_list *_xs_json_loads_array(const char **json, js_type *t);
static xs_dict *_xs_json_loads_object(const char **json, js_type *t);
2022-09-19 18:41:11 +00:00
2023-02-28 10:22:52 +00:00
static xs_val *_xs_json_loads_value(const char **json, js_type *t, xs_val *v)
2022-09-19 18:41:11 +00:00
/* parses a JSON value */
{
if (*t == JS_OBRACK)
v = _xs_json_loads_array(json, t);
else
if (*t == JS_OCURLY)
v = _xs_json_loads_object(json, t);
if (*t >= JS_VALUE)
*t = JS_VALUE;
else
*t = JS_ERROR;
return v;
}
2023-02-28 10:22:52 +00:00
static xs_list *_xs_json_loads_array(const char **json, js_type *t)
2022-09-19 18:41:11 +00:00
/* parses a JSON array */
{
const char *s = *json;
xs *v;
2023-02-28 10:22:52 +00:00
xs_list *l;
2022-09-19 18:41:11 +00:00
js_type tt;
l = xs_list_new();
*t = JS_INCOMPLETE;
v = _xs_json_loads_lexer(&s, &tt);
if (tt == JS_CBRACK)
*t = JS_ARRAY;
else {
v = _xs_json_loads_value(&s, &tt, v);
if (tt == JS_VALUE) {
l = xs_list_append(l, v);
while (*t == JS_INCOMPLETE) {
2022-10-25 07:32:41 +00:00
xs_free(_xs_json_loads_lexer(&s, &tt));
2022-09-19 18:41:11 +00:00
if (tt == JS_CBRACK)
*t = JS_ARRAY;
else
if (tt == JS_COMMA) {
xs *v2;
v2 = _xs_json_loads_lexer(&s, &tt);
v2 = _xs_json_loads_value(&s, &tt, v2);
if (tt == JS_VALUE)
l = xs_list_append(l, v2);
else
*t = JS_ERROR;
}
else
*t = JS_ERROR;
}
}
else
*t = JS_ERROR;
}
2022-10-25 07:32:41 +00:00
if (*t == JS_ERROR)
l = xs_free(l);
2022-09-19 18:41:11 +00:00
*json = s;
return l;
}
2023-02-28 10:22:52 +00:00
static xs_dict *_xs_json_loads_object(const char **json, js_type *t)
2022-09-19 18:41:11 +00:00
/* parses a JSON object */
{
const char *s = *json;
xs *k1;
2023-02-28 10:22:52 +00:00
xs_dict *d;
2022-09-19 18:41:11 +00:00
js_type tt;
d = xs_dict_new();
*t = JS_INCOMPLETE;
k1 = _xs_json_loads_lexer(&s, &tt);
if (tt == JS_CCURLY)
*t = JS_OBJECT;
else
if (tt == JS_STRING) {
2022-10-25 07:32:41 +00:00
xs_free(_xs_json_loads_lexer(&s, &tt));
2022-09-19 18:41:11 +00:00
if (tt == JS_COLON) {
xs *v1;
v1 = _xs_json_loads_lexer(&s, &tt);
v1 = _xs_json_loads_value(&s, &tt, v1);
if (tt == JS_VALUE) {
d = xs_dict_append(d, k1, v1);
while (*t == JS_INCOMPLETE) {
2022-10-25 07:32:41 +00:00
xs_free(_xs_json_loads_lexer(&s, &tt));
2022-09-19 18:41:11 +00:00
if (tt == JS_CCURLY)
*t = JS_OBJECT;
else
if (tt == JS_COMMA) {
2022-09-20 05:46:58 +00:00
xs *k = _xs_json_loads_lexer(&s, &tt);
2022-09-19 18:41:11 +00:00
if (tt == JS_STRING) {
2022-10-25 07:32:41 +00:00
xs_free(_xs_json_loads_lexer(&s, &tt));
2022-09-19 18:41:11 +00:00
if (tt == JS_COLON) {
xs *v;
v = _xs_json_loads_lexer(&s, &tt);
v = _xs_json_loads_value(&s, &tt, v);
if (tt == JS_VALUE)
d = xs_dict_append(d, k, v);
else
*t = JS_ERROR;
}
else
*t = JS_ERROR;
}
else
*t = JS_ERROR;
}
else
*t = JS_ERROR;
}
}
else
*t = JS_ERROR;
}
else
*t = JS_ERROR;
}
else
*t = JS_ERROR;
2022-10-25 07:32:41 +00:00
if (*t == JS_ERROR)
d = xs_free(d);
2022-09-19 18:41:11 +00:00
*json = s;
return d;
}
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-01-28 16:49:02 +00:00
xs_val *v = NULL;
2022-09-19 18:41:11 +00:00
js_type t;
2022-10-25 07:32:41 +00:00
xs_free(_xs_json_loads_lexer(&json, &t));
2022-09-19 18:41:11 +00:00
if (t == JS_OBRACK)
v = _xs_json_loads_array(&json, &t);
else
if (t == JS_OCURLY)
v = _xs_json_loads_object(&json, &t);
else
t = JS_ERROR;
return v;
}
xs_val *xs_json_load(FILE *f)
/* loads a JSON file */
{
xs *o = xs_readall(f);
return o ? xs_json_loads(o) : NULL;
}
2022-09-19 18:41:11 +00:00
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_JSON_H */