Browse Source Download (without any required ccan dependencies)
json_out
Code for creating simple JSON output.
Rusty Russell <[email protected]>
This code helps you create well-formed JSON strings.
// Given "a 1 true" outputs {"argv1":"a","argv2":1,"argv3":true}
// Print arguments as a JSON array.
#include <ccan/json_out/json_out.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
// Simplistic test to see if str needs quotes.
static bool can_be_json_literal(const char *str)
{
char *endp;
if (strtol(str, &endp, 10) != LONG_MIN
&& endp != str
&& *endp == '\0')
return true;
return !strcmp(str, "true")
|| !strcmp(str, "false")
|| !strcmp(str, "null");
}
int main(int argc, char *argv[])
{
struct json_out *jout = json_out_new(NULL);
size_t len;
const char *p;
json_out_start(jout, NULL, '{');
for (int i = 1; i < argc; i++) {
char fieldname[80];
sprintf(fieldname, "argv%i", i);
json_out_add(jout, fieldname,
!can_be_json_literal(argv[i]),
"%s", argv[i]);
}
json_out_end(jout, '}');
// Force appending of \n
json_out_direct(jout, 1)[0] = '\n';
json_out_finished(jout);
// Now write it out.
while ((p = json_out_contents(jout, &len)) != NULL) {
int i = write(STDOUT_FILENO, p, len);
if (i <= 0)
exit(1);
json_out_consume(jout, i);
}
tal_free(jout);
return 0;
}
BSD-MIT