2020-02-08 02:49:22 +01:00
|
|
|
#include <inttypes.h>
|
2012-07-23 12:39:45 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <talloc.h>
|
|
|
|
#include "sprinter.h"
|
|
|
|
|
|
|
|
struct sprinter_json {
|
|
|
|
struct sprinter vtable;
|
|
|
|
FILE *stream;
|
|
|
|
/* Top of the state stack, or NULL if the printer is not currently
|
|
|
|
* inside any aggregate types. */
|
|
|
|
struct json_state *state;
|
|
|
|
|
|
|
|
/* A flag to signify that a separator should be inserted in the
|
|
|
|
* output as soon as possible.
|
|
|
|
*/
|
2017-10-07 10:44:04 +02:00
|
|
|
bool insert_separator;
|
2012-07-23 12:39:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct json_state {
|
|
|
|
struct json_state *parent;
|
|
|
|
/* True if nothing has been printed in this aggregate yet.
|
|
|
|
* Suppresses the comma before a value. */
|
2017-10-07 10:44:04 +02:00
|
|
|
bool first;
|
2012-07-23 12:39:45 +02:00
|
|
|
/* The character that closes the current aggregate. */
|
|
|
|
char close;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Helper function to set up the stream to print a value. If this
|
|
|
|
* value follows another value, prints a comma. */
|
|
|
|
static struct sprinter_json *
|
|
|
|
json_begin_value (struct sprinter *sp)
|
|
|
|
{
|
|
|
|
struct sprinter_json *spj = (struct sprinter_json *) sp;
|
|
|
|
|
|
|
|
if (spj->state) {
|
|
|
|
if (! spj->state->first) {
|
|
|
|
fputc (',', spj->stream);
|
|
|
|
if (spj->insert_separator) {
|
|
|
|
fputc ('\n', spj->stream);
|
2017-10-07 10:44:04 +02:00
|
|
|
spj->insert_separator = false;
|
2012-07-23 12:39:45 +02:00
|
|
|
} else {
|
|
|
|
fputc (' ', spj->stream);
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-07 10:44:04 +02:00
|
|
|
spj->state->first = false;
|
2012-07-23 12:39:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return spj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function to begin an aggregate type. Prints the open
|
|
|
|
* character and pushes a new state frame. */
|
|
|
|
static void
|
|
|
|
json_begin_aggregate (struct sprinter *sp, char open, char close)
|
|
|
|
{
|
|
|
|
struct sprinter_json *spj = json_begin_value (sp);
|
|
|
|
struct json_state *state = talloc (spj, struct json_state);
|
|
|
|
|
|
|
|
fputc (open, spj->stream);
|
|
|
|
state->parent = spj->state;
|
2017-10-07 10:44:04 +02:00
|
|
|
state->first = true;
|
2012-07-23 12:39:45 +02:00
|
|
|
state->close = close;
|
|
|
|
spj->state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
json_begin_map (struct sprinter *sp)
|
|
|
|
{
|
|
|
|
json_begin_aggregate (sp, '{', '}');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
json_begin_list (struct sprinter *sp)
|
|
|
|
{
|
|
|
|
json_begin_aggregate (sp, '[', ']');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
json_end (struct sprinter *sp)
|
|
|
|
{
|
|
|
|
struct sprinter_json *spj = (struct sprinter_json *) sp;
|
|
|
|
struct json_state *state = spj->state;
|
|
|
|
|
|
|
|
fputc (spj->state->close, spj->stream);
|
|
|
|
spj->state = state->parent;
|
|
|
|
talloc_free (state);
|
|
|
|
if (spj->state == NULL)
|
|
|
|
fputc ('\n', spj->stream);
|
|
|
|
}
|
|
|
|
|
2012-08-03 03:14:49 +02:00
|
|
|
/* This implementation supports embedded NULs as allowed by the JSON
|
|
|
|
* specification and Unicode. Support for *parsing* embedded NULs
|
|
|
|
* varies, but is generally not a problem outside of C-based parsers
|
|
|
|
* (Python's json module and Emacs' json.el take embedded NULs in
|
|
|
|
* stride). */
|
2012-07-23 12:39:45 +02:00
|
|
|
static void
|
2012-08-03 03:14:49 +02:00
|
|
|
json_string_len (struct sprinter *sp, const char *val, size_t len)
|
2012-07-23 12:39:45 +02:00
|
|
|
{
|
|
|
|
static const char *const escapes[] = {
|
|
|
|
['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
|
|
|
|
['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t"
|
|
|
|
};
|
|
|
|
struct sprinter_json *spj = json_begin_value (sp);
|
|
|
|
|
|
|
|
fputc ('"', spj->stream);
|
2012-08-03 03:14:49 +02:00
|
|
|
for (; len; ++val, --len) {
|
2012-07-23 12:39:45 +02:00
|
|
|
unsigned char ch = *val;
|
|
|
|
if (ch < ARRAY_SIZE (escapes) && escapes[ch])
|
|
|
|
fputs (escapes[ch], spj->stream);
|
|
|
|
else if (ch >= 32)
|
|
|
|
fputc (ch, spj->stream);
|
|
|
|
else
|
|
|
|
fprintf (spj->stream, "\\u%04x", ch);
|
|
|
|
}
|
|
|
|
fputc ('"', spj->stream);
|
|
|
|
}
|
|
|
|
|
2012-08-03 03:14:49 +02:00
|
|
|
static void
|
|
|
|
json_string (struct sprinter *sp, const char *val)
|
|
|
|
{
|
2012-08-08 23:23:33 +02:00
|
|
|
if (val == NULL)
|
|
|
|
val = "";
|
2012-08-03 03:14:49 +02:00
|
|
|
json_string_len (sp, val, strlen (val));
|
|
|
|
}
|
|
|
|
|
2012-07-23 12:39:45 +02:00
|
|
|
static void
|
2020-02-08 02:49:22 +01:00
|
|
|
json_integer (struct sprinter *sp, int64_t val)
|
2012-07-23 12:39:45 +02:00
|
|
|
{
|
|
|
|
struct sprinter_json *spj = json_begin_value (sp);
|
|
|
|
|
2021-03-13 13:45:34 +01:00
|
|
|
fprintf (spj->stream, "%" PRId64, val);
|
2012-07-23 12:39:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-10-07 10:44:04 +02:00
|
|
|
json_boolean (struct sprinter *sp, bool val)
|
2012-07-23 12:39:45 +02:00
|
|
|
{
|
|
|
|
struct sprinter_json *spj = json_begin_value (sp);
|
|
|
|
|
|
|
|
fputs (val ? "true" : "false", spj->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
json_null (struct sprinter *sp)
|
|
|
|
{
|
|
|
|
struct sprinter_json *spj = json_begin_value (sp);
|
|
|
|
|
|
|
|
fputs ("null", spj->stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
json_map_key (struct sprinter *sp, const char *key)
|
|
|
|
{
|
|
|
|
struct sprinter_json *spj = (struct sprinter_json *) sp;
|
|
|
|
|
|
|
|
json_string (sp, key);
|
|
|
|
fputs (": ", spj->stream);
|
2017-10-07 10:44:04 +02:00
|
|
|
spj->state->first = true;
|
2012-07-23 12:39:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
json_set_prefix (unused (struct sprinter *sp), unused (const char *name))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
json_separator (struct sprinter *sp)
|
|
|
|
{
|
|
|
|
struct sprinter_json *spj = (struct sprinter_json *) sp;
|
|
|
|
|
2017-10-07 10:44:04 +02:00
|
|
|
spj->insert_separator = true;
|
2012-07-23 12:39:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sprinter *
|
|
|
|
sprinter_json_create (const void *ctx, FILE *stream)
|
|
|
|
{
|
|
|
|
static const struct sprinter_json template = {
|
|
|
|
.vtable = {
|
|
|
|
.begin_map = json_begin_map,
|
|
|
|
.begin_list = json_begin_list,
|
|
|
|
.end = json_end,
|
|
|
|
.string = json_string,
|
2012-08-03 03:14:49 +02:00
|
|
|
.string_len = json_string_len,
|
2012-07-23 12:39:45 +02:00
|
|
|
.integer = json_integer,
|
|
|
|
.boolean = json_boolean,
|
|
|
|
.null = json_null,
|
|
|
|
.map_key = json_map_key,
|
|
|
|
.separator = json_separator,
|
|
|
|
.set_prefix = json_set_prefix,
|
2017-10-07 10:44:04 +02:00
|
|
|
.is_text_printer = false,
|
2012-07-23 12:39:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
struct sprinter_json *res;
|
|
|
|
|
|
|
|
res = talloc (ctx, struct sprinter_json);
|
|
|
|
if (! res)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*res = template;
|
|
|
|
res->stream = stream;
|
|
|
|
return &res->vtable;
|
|
|
|
}
|