]> git.ozlabs.org Git - ccan/blob - ccan/json_out/_info
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / json_out / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * json_out - Code for creating simple JSON output.
7  *
8  * This code helps you create well-formed JSON strings.
9  *
10  * Author: Rusty Russell <rusty@rustcorp.com.au>
11  * License: BSD-MIT
12  *
13  * Example:
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>
17  *      #include <stdio.h>
18  *      #include <string.h>
19  *      #include <unistd.h>
20  *
21  *      // Simplistic test to see if str needs quotes.
22  *      static bool can_be_json_literal(const char *str)
23  *      {
24  *              char *endp;
25  *              if (strtol(str, &endp, 10) != LONG_MIN
26  *                  && endp != str
27  *                  && *endp == '\0')
28  *                      return true;
29  *              return !strcmp(str, "true")
30  *                      || !strcmp(str, "false")
31  *                      || !strcmp(str, "null");
32  *      }
33  *              
34  *      int main(int argc, char *argv[])
35  *      {
36  *              struct json_out *jout = json_out_new(NULL);
37  *              size_t len;
38  *              const char *p;
39  *
40  *              json_out_start(jout, NULL, '{');
41  *              for (int i = 1; i < argc; i++) {
42  *                      char fieldname[80];
43  *                      sprintf(fieldname, "argv%i", i);
44  *                      json_out_add(jout, fieldname,
45  *                                   !can_be_json_literal(argv[i]),
46  *                                   "%s", argv[i]);
47  *              }
48  *              json_out_end(jout, '}');
49  *              // Force appending of \n
50  *              json_out_direct(jout, 1)[0] = '\n';
51  *              json_out_finished(jout);
52  *
53  *              // Now write it out.
54  *              while ((p = json_out_contents(jout, &len)) != NULL) {
55  *                      int i = write(STDOUT_FILENO, p, len);
56  *                      if (i <= 0)
57  *                              exit(1);
58  *                      json_out_consume(jout, i);
59  *              }
60  *
61  *              tal_free(jout);
62  *              return 0;
63  *      }
64  *      
65  */
66 int main(int argc, char *argv[])
67 {
68         /* Expect exactly one argument */
69         if (argc != 2)
70                 return 1;
71
72         if (strcmp(argv[1], "depends") == 0) {
73                 printf("ccan/compiler\n");
74                 printf("ccan/json_escape\n");
75                 printf("ccan/membuf\n");
76                 printf("ccan/tal\n");
77                 printf("ccan/typesafe_cb\n");
78                 return 0;
79         }
80
81         return 1;
82 }