-/* $OpenBSD: json.c,v 1.4 2023/04/26 17:39:20 tb Exp $ */
+/* $OpenBSD: json.c,v 1.5 2023/04/26 19:14:54 claudio Exp $ */
/*
* Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
OBJECT
};
-struct json_stack {
+static struct json_stack {
const char *name;
unsigned int count;
enum json_type type;
} stack[JSON_MAX_STACK];
-char indent[JSON_MAX_STACK + 1];
-int level;
+static char indent[JSON_MAX_STACK + 1];
+static int level;
+static FILE *jsonfh;
static void
do_comma_indent(void)
{
if (stack[level].count++ > 0)
- printf(",\n");
- printf("\t%.*s", level, indent);
+ fprintf(jsonfh, ",\n");
+ fprintf(jsonfh, "\t%.*s", level, indent);
}
static void
{
if (stack[level].type == ARRAY)
return;
- printf("\"%s\": ", name);
+ fprintf(jsonfh, "\"%s\": ", name);
}
static int
}
void
-json_do_start(void)
+json_do_start(FILE *fh)
{
memset(indent, '\t', JSON_MAX_STACK);
memset(stack, 0, sizeof(stack));
level = 0;
stack[level].type = START;
+ jsonfh = fh;
- printf("{\n");
+ fprintf(jsonfh, "{\n");
}
void
{
while (level > 0)
json_do_end();
- printf("\n}\n");
+ fprintf(jsonfh, "\n}\n");
}
void
do_comma_indent();
do_name(name);
- printf("[\n");
+ fprintf(jsonfh, "[\n");
if (++level >= JSON_MAX_STACK)
errx(1, "json stack too deep");
do_comma_indent();
do_name(name);
- printf("{\n");
+ fprintf(jsonfh, "{\n");
if (++level >= JSON_MAX_STACK)
errx(1, "json stack too deep");
json_do_end(void)
{
if (stack[level].type == ARRAY)
- printf("\n%.*s]", level, indent);
+ fprintf(jsonfh, "\n%.*s]", level, indent);
else if (stack[level].type == OBJECT)
- printf("\n%.*s}", level, indent);
+ fprintf(jsonfh, "\n%.*s}", level, indent);
else
errx(1, "json bad stack state");
do_comma_indent();
do_name(name);
- printf("\"");
+ fprintf(jsonfh, "\"");
va_start(ap, fmt);
- vprintf(fmt, ap);
+ vfprintf(jsonfh, fmt, ap);
va_end(ap);
- printf("\"");
+ fprintf(jsonfh, "\"");
}
void
do_comma_indent();
do_name(name);
- printf("\"");
+ fprintf(jsonfh, "\"");
for (i = 0; i < len; i++)
- printf("%02x", *(data+i));
- printf("\"");
+ fprintf(jsonfh, "%02x", *(data + i));
+ fprintf(jsonfh, "\"");
}
void
do_comma_indent();
do_name(name);
if (v)
- printf("true");
+ fprintf(jsonfh, "true");
else
- printf("false");
+ fprintf(jsonfh, "false");
}
void
{
do_comma_indent();
do_name(name);
- printf("%llu", v);
+ fprintf(jsonfh, "%llu", v);
}
void
{
do_comma_indent();
do_name(name);
- printf("%lld", v);
+ fprintf(jsonfh, "%lld", v);
}
void
{
do_comma_indent();
do_name(name);
- printf("%f", v);
+ fprintf(jsonfh, "%f", v);
}