This commit is contained in:
Love Hornquist Astrand
2011-12-15 21:48:20 -08:00
parent d05e64b967
commit b780dddb9b

View File

@@ -36,68 +36,117 @@
#include "baselocl.h" #include "baselocl.h"
#include <ctype.h> #include <ctype.h>
#if 0 struct twojson {
int void *ctx;
heim_base2json(heim_object_t obj, size_t indent;
void (*out)(const char *, void *), void *ctx); void (*out)(void *, const char *);
};
int static int
heim_base2json(heim_object_t obj, base2json(heim_object_t, struct twojson *);
void (*out)(const char *, void *), void *ctx)
static void
indent(struct twojson *j)
{ {
heim_tid_t type = heim_get_tid(obj); size_t indent = j->indent;
__block int fail = 0, needcomma = 0; while (indent--)
j->out(j->ctx, "\t");
}
static void
array2json(heim_object_t value, void *ctx)
{
struct twojson *j = ctx;
indent(j);
base2json(value, j);
j->out(j->ctx, ",\n");
j->indent--;
}
static void
dict2json(heim_object_t key, heim_object_t value, void *ctx)
{
struct twojson *j = ctx;
indent(j);
base2json(key, j);
j->out(j->ctx, " = ");
base2json(value, j);
j->out(j->ctx, ",\n");
}
static int
base2json(heim_object_t obj, struct twojson *j)
{
heim_tid_t type;
if (obj == NULL) {
indent(j);
j->out(j->ctx, "<NULL>\n");
}
type = heim_get_tid(obj);
switch (type) { switch (type) {
case HEIM_TID_ARRAY: case HEIM_TID_ARRAY:
out("[ ", ctx); indent(j);
heim_array_iterate(obj, ^(heim_object_t sub) { j->out(j->ctx, "[\n");
if (needcomma) j->indent++;
out(", ", ctx); heim_array_iterate_f(obj, j, array2json);
fail |= heim_base2json(sub, out, ctx); j->indent--;
needcomma = 1; indent(j);
}); j->out(j->ctx, "]\n");
out("]", ctx);
break; break;
case HEIM_TID_DICT: case HEIM_TID_DICT:
out("{ ", ctx); indent(j);
heim_dict_iterate(obj, ^(heim_object_t key, heim_object_t value) { j->out(j->ctx, "{\n");
if (needcomma) j->indent++;
out(", ", ctx); heim_dict_iterate_f(obj, j, dict2json);
fail |= heim_base2json(key, out, ctx); j->indent--;
out(" = ", ctx); indent(j);
fail |= heim_base2json(value, out, ctx); j->out(j->ctx, "}\n");
needcomma = 1;
});
out("}", ctx);
break; break;
case HEIM_TID_STRING: case HEIM_TID_STRING:
out("\"", ctx); indent(j);
out(heim_string_get_utf8(obj), ctx); j->out(j->ctx, "\"");
out("\"", ctx); j->out(j->ctx, heim_string_get_utf8(obj));
j->out(j->ctx, "\"");
break; break;
case HEIM_TID_NUMBER: { case HEIM_TID_NUMBER: {
char num[16]; char num[16];
indent(j);
snprintf(num, sizeof(num), "%d", heim_number_get_int(obj)); snprintf(num, sizeof(num), "%d", heim_number_get_int(obj));
out(num, ctx); j->out(j->ctx, num);
break; break;
} }
case HEIM_TID_NULL: case HEIM_TID_NULL:
out("null", ctx); indent(j);
j->out(j->ctx, "null");
break; break;
case HEIM_TID_BOOL: case HEIM_TID_BOOL:
out(heim_bool_val(obj) ? "true" : "false", ctx); indent(j);
j->out(j->ctx, heim_bool_val(obj) ? "true" : "false");
break; break;
default: default:
return 1; return 1;
} }
return fail; return 0;
}
static int
heim_base2json(heim_object_t obj, void *ctx,
void (*out)(void *, const char *))
{
struct twojson j;
j.indent = 0;
j.ctx = ctx;
j.out = out;
return base2json(obj, &j);
} }
#endif
/* /*
* *
@@ -397,3 +446,16 @@ heim_json_create_with_bytes(const void *data, size_t length, heim_error_t *error
return o; return o;
} }
static void
show_printf(void *ctx, const char *str)
{
fprintf(ctx, "%s", str);
}
void
heim_show(heim_object_t obj)
{
heim_base2json(obj, stderr, show_printf);
}