snac2/xs_json.h

472 lines
9.3 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
int xs_json_dump(const xs_val *data, int indent, FILE *f);
xs_str *xs_json_dumps(const xs_val *data, int indent);
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;
}
}
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 **/
/* 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,
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;
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 */
while ((c = fgetc(f)) == L' ' || c == L'\t' || c == L'\n' || c == L'\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);
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-08-08 11:04:55 +00:00
if (cp >= 0xd800 && cp <= 0xdfff) {
/* it's a surrogate pair */
2023-08-08 11:04:55 +00:00
cp = (cp & 0x3ff) << 10;
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-08-08 11:04:55 +00:00
unsigned int i;
if (fscanf(f, "%04x", &i) != 1) {
2023-05-15 08:31:15 +00:00
*t = JS_ERROR;
break;
}
cp |= (i & 0x3ff);
cp += 0x10000;
}
/* 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;
}
2023-08-08 11:04:55 +00:00
v = xs_utf8_enc(v, cp);
}
else {
char cc = c;
v = xs_append_m(v, &cc, 1);
}
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;
}
2023-08-08 11:04:55 +00:00
static xs_list *_xs_json_load_array(FILE *f, js_type *t);
static xs_dict *_xs_json_load_object(FILE *f, js_type *t);
2022-09-19 18:41:11 +00:00
2023-08-08 11:04:55 +00:00
static xs_list *_xs_json_load_array(FILE *f, js_type *t)
2022-09-19 18:41:11 +00:00
/* parses a JSON array */
{
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
*t = JS_INCOMPLETE;
2023-08-12 06:37:31 +00:00
while (*t == JS_INCOMPLETE) {
js_type tt;
xs *v = _xs_json_load_lexer(f, &tt);
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt == JS_ERROR)
break;
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt == JS_CBRACK) {
*t = JS_ARRAY;
break;
}
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (c > 0) {
if (tt == JS_COMMA)
v = _xs_json_load_lexer(f, &tt);
else
break;
}
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt == JS_OBRACK)
v = _xs_json_load_array(f, &tt);
else
if (tt == JS_OCURLY)
v = _xs_json_load_object(f, &tt);
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt < JS_VALUE)
break;
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
l = xs_list_append(l, v);
c++;
2022-09-19 18:41:11 +00:00
}
2023-08-12 06:37:31 +00:00
if (*t == JS_INCOMPLETE || *t == JS_ERROR)
2022-10-25 07:32:41 +00:00
l = xs_free(l);
2022-09-19 18:41:11 +00:00
return l;
}
2023-08-08 11:04:55 +00:00
static xs_dict *_xs_json_load_object(FILE *f, js_type *t)
2022-09-19 18:41:11 +00:00
/* parses a JSON object */
{
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
d = xs_dict_new();
*t = JS_INCOMPLETE;
2023-08-12 06:37:31 +00:00
while (*t == JS_INCOMPLETE) {
js_type tt;
xs *k = _xs_json_load_lexer(f, &tt);
xs *v = NULL;
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt == JS_ERROR)
break;
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt == JS_CCURLY) {
*t = JS_OBJECT;
break;
}
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (c > 0) {
if (tt == JS_COMMA)
k = _xs_json_load_lexer(f, &tt);
else
break;
}
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt != JS_STRING)
break;
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
xs_free(_xs_json_load_lexer(f, &tt));
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt != JS_COLON)
break;
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
v = _xs_json_load_lexer(f, &tt);
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt == JS_OBRACK)
v = _xs_json_load_array(f, &tt);
else
if (tt == JS_OCURLY)
v = _xs_json_load_object(f, &tt);
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
if (tt < JS_VALUE)
break;
2022-09-19 18:41:11 +00:00
2023-08-12 06:37:31 +00:00
d = xs_dict_append(d, k, v);
c++;
2022-09-19 18:41:11 +00:00
}
2023-08-12 06:37:31 +00:00
if (*t == JS_INCOMPLETE || *t == JS_ERROR)
2022-10-25 07:32:41 +00:00
d = xs_free(d);
2022-09-19 18:41:11 +00:00
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-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;
}
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;
2022-09-19 18:41:11 +00:00
js_type t;
2023-08-08 11:04:55 +00:00
xs_free(_xs_json_load_lexer(f, &t));
2022-09-19 18:41:11 +00:00
if (t == JS_OBRACK)
2023-08-08 11:04:55 +00:00
v = _xs_json_load_array(f, &t);
2022-09-19 18:41:11 +00:00
else
if (t == JS_OCURLY)
2023-08-08 11:04:55 +00:00
v = _xs_json_load_object(f, &t);
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 */