89 lines
2.5 KiB
C
89 lines
2.5 KiB
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
|
|
#include "json.h"
|
|
|
|
jsonval_t json_createobj(jsonkv_dynarr_t pairs) {
|
|
jsonkv_dynarr_t *stolen = xmalloc(sizeof(jsonkv_dynarr_t));
|
|
*stolen = pairs;
|
|
return (jsonval_t){ .type = JSON_OBJECT, .data = stolen };
|
|
}
|
|
|
|
jsonval_t json_createarr(jsonval_dynarr_t elems) {
|
|
jsonval_dynarr_t *stolen = xmalloc(sizeof(jsonval_dynarr_t));
|
|
*stolen = elems;
|
|
return (jsonval_t){ .type = JSON_ARRAY, .data = stolen };
|
|
}
|
|
|
|
jsonval_t json_createstr(const char *str) {
|
|
return (jsonval_t){ .type = JSON_STRING, .data = (void*)str };
|
|
}
|
|
|
|
jsonval_t json_createint(long num) {
|
|
return (jsonval_t){ .type = JSON_INT, .data = (void*)num };
|
|
}
|
|
|
|
jsonval_t json_createbool(bool val) {
|
|
return (jsonval_t){ .type = JSON_BOOL, .data = (void*)val };
|
|
}
|
|
|
|
jsonval_t json_createnull(void) {
|
|
return (jsonval_t){ .type = JSON_NULL };
|
|
}
|
|
|
|
void json_destroy(jsonval_t *val) {
|
|
if (val->type == JSON_OBJECT) {
|
|
jsonkv_dynarr_t *arr = (jsonkv_dynarr_t*)val->data;
|
|
for (size_t i = 0; i < arr->len; i++)
|
|
json_destroy(&arr->data[i].val);
|
|
dynarr_destroy(*arr);
|
|
xfree(arr);
|
|
}
|
|
else if (val->type == JSON_ARRAY) {
|
|
jsonval_dynarr_t *arr = (jsonval_dynarr_t*)val->data;
|
|
for (size_t i = 0; i < arr->len; i++)
|
|
json_destroy(&arr->data[i]);
|
|
dynarr_destroy(*arr);
|
|
xfree(arr);
|
|
}
|
|
}
|
|
|
|
void json_write(FILE *out, jsonval_t *val) {
|
|
switch(val->type) {
|
|
case JSON_OBJECT:
|
|
fprintf(out, "{");
|
|
jsonkv_dynarr_t *map = (jsonkv_dynarr_t*)val->data;
|
|
for (size_t i = 0; i < map->len; i++) {
|
|
jsonkv_t *pair = &map->data[i];
|
|
fprintf(out, "\"%s\":", pair->key);
|
|
json_write(out, &pair->val);
|
|
if (i != map->len - 1)
|
|
fprintf(out, ",");
|
|
}
|
|
fprintf(out, "}");
|
|
break;
|
|
case JSON_ARRAY:
|
|
fprintf(out, "[");
|
|
jsonval_dynarr_t *arr = (jsonval_dynarr_t*)val->data;
|
|
for (size_t i = 0; i < arr->len; i++) {
|
|
json_write(out, &arr->data[i]);
|
|
if (i != arr->len - 1)
|
|
fprintf(out, ",");
|
|
}
|
|
fprintf(out, "]");
|
|
break;
|
|
case JSON_STRING:
|
|
fprintf(out, "\"%s\"", (const char*)val->data);
|
|
break;
|
|
case JSON_INT:
|
|
fprintf(out, "%ld", (long)val->data);
|
|
break;
|
|
case JSON_BOOL:
|
|
fprintf(out, "%s", val->data ? "true" : "false");
|
|
break;
|
|
case JSON_NULL:
|
|
fprintf(out, "null");
|
|
break;
|
|
}
|
|
}
|