6 * json_out - Code for creating simple JSON output.
8 * This code helps you create well-formed JSON strings.
10 * Author: Rusty Russell <rusty@rustcorp.com.au>
14 * // Given "a 1 true" outputs {"argv1":"a","argv2":1,"argv3":true}
15 * // Print arguments as a JSON array.
16 * #include <ccan/json_out/json_out.h>
21 * // Simplistic test to see if str needs quotes.
22 * static bool can_be_json_literal(const char *str)
25 * if (strtol(str, &endp, 10) != LONG_MIN
29 * return !strcmp(str, "true")
30 * || !strcmp(str, "false")
31 * || !strcmp(str, "null");
34 * int main(int argc, char *argv[])
36 * struct json_out *jout = json_out_new(NULL);
40 * json_out_start(jout, NULL, '{');
41 * for (int i = 1; i < argc; i++) {
43 * sprintf(fieldname, "argv%i", i);
44 * json_out_add(jout, fieldname,
45 * !can_be_json_literal(argv[i]),
48 * json_out_end(jout, '}');
49 * // Force appending of \n
50 * json_out_direct(jout, 1)[0] = '\n';
51 * json_out_finished(jout);
53 * // Now write it out.
54 * while ((p = json_out_contents(jout, &len)) != NULL) {
55 * int i = write(STDOUT_FILENO, p, len);
58 * json_out_consume(jout, i);
66 int main(int argc, char *argv[])
68 /* Expect exactly one argument */
72 if (strcmp(argv[1], "depends") == 0) {
73 printf("ccan/compiler\n");
74 printf("ccan/json_escape\n");
75 printf("ccan/membuf\n");
77 printf("ccan/typesafe_cb\n");