#include "baselocl.h"
int
heim_base2json(heim_object_t obj,
void (*out)(char *, void *), void *ctx)
{
heim_tid_t type = heim_get_tid(obj);
__block int fail = 0, needcomma = 0;
switch (type) {
case HEIM_TID_ARRAY:
out("[ ", ctx);
heim_array_iterate(obj, ^(heim_object_t sub) {
if (needcomma)
out(", ", ctx);
fail |= heim_base2json(sub, out, ctx);
needcomma = 1;
});
out("]", ctx);
break;
case HEIM_TID_DICT:
out("{ ", ctx);
heim_dict_iterate(obj, ^(heim_object_t key, heim_object_t value) {
if (needcomma)
out(", ", ctx);
fail |= heim_base2json(key, out, ctx);
out(" = ", ctx);
fail |= heim_base2json(value, out, ctx);
needcomma = 1;
});
out("}", ctx);
break;
case HEIM_TID_STRING:
out("\"", ctx);
out(heim_string_get_utf8(obj), ctx);
out("\"", ctx);
break;
case HEIM_TID_NUMBER: {
char num[16];
snprintf(num, sizeof(num), "%d", heim_number_get_int(obj));
out(num, ctx);
break;
}
case HEIM_TID_NULL:
out("null", ctx);
break;
case HEIM_TID_BOOL:
out(heim_bool_val(obj) ? "true" : "false", ctx);
break;
default:
return 1;
}
return fail;
}
static int
parse_dict(heim_dict_t dict, char * const *pp, size_t *len)
{
const char *p = *pp;
while (*len) {
(*len)--;
if (*p == '\n') {
p += 1;
} else if (isspace(*p)) {
p += 1;
} else if (*p == '}') {
*pp = p + 1;
return 0;
} else {
}
}
return ENOENT;
}
heim_object_t
heim_json2base(const void *data, size_t length)
{
heim_array_t stack;
heim_object_t o = NULL;
const char *p = data;
unsigned long lineno = 1;
while (length) {
length--;
if (*p == '\n') {
lineno++;
} else if (isspace((int)*p)) {
;
} else if (*p == '{') {
o = heim_dict_create();
if ((ret = parse_dict(&p, &length)) != 0)
goto out;
} else
abort();
}
out:
if (ret && o) {
heim_release(o);
o = NULL;
}
return o;
}