6 * json - Parse and generate JSON (JavaScript Object Notation)
8 * This is a library for encoding and decoding JSON that strives to be
9 * easy to learn, use, and incorporate into an application.
11 * JSON (JavaScript Object Notation) facilitates passing data among different
12 * programming languages, particularly JavaScript. It looks like this:
17 * "firstname": "John",
18 * "lastname": "Smith",
19 * "email": "john@example.com",
20 * "likes_pizza": false
24 * "firstname": "Linda",
25 * "lastname": "Jones",
32 * #include <ccan/json/json.h>
37 * static int find_number(JsonNode *object, const char *name, double *out)
39 * JsonNode *node = json_find_member(object, name);
40 * if (node && node->tag == JSON_NUMBER) {
41 * *out = node->number_;
47 * static void solve_pythagorean(JsonNode *triple)
49 * double a = 0, b = 0, c = 0;
50 * int a_given, b_given, c_given;
52 * if (triple->tag != JSON_OBJECT) {
53 * fprintf(stderr, "Error: Expected a JSON object.\n");
57 * a_given = find_number(triple, "a", &a);
58 * b_given = find_number(triple, "b", &b);
59 * c_given = find_number(triple, "c", &c);
61 * if (a_given + b_given + c_given != 2) {
62 * fprintf(stderr, "Error: I need two sides to compute the length of the third.\n");
66 * if (a_given && b_given) {
67 * c = sqrt(a*a + b*b);
68 * json_append_member(triple, "c", json_mknumber(c));
69 * } else if (a_given && c_given) {
70 * b = sqrt(c*c - a*a);
71 * json_append_member(triple, "b", json_mknumber(b));
72 * } else if (b_given && c_given) {
73 * a = sqrt(c*c - b*b);
74 * json_append_member(triple, "a", json_mknumber(a));
80 * JsonNode *triples = json_mkarray();
82 * json_append_element(triples, json_decode("{\"a\": 3, \"b\": 4}"));
83 * json_append_element(triples, json_decode("{\"a\": 5, \"c\": 13}"));
84 * json_append_element(triples, json_decode("{\"b\": 24, \"c\": 25}"));
87 * json_foreach(triple, triples)
88 * solve_pythagorean(triple);
90 * char *tmp = json_stringify(triples, "\t");
94 * json_delete(triples);
102 int main(int argc, char *argv[])
104 /* Expect exactly one argument */
108 if (strcmp(argv[1], "depends") == 0) {
113 if (strcmp(argv[1], "libs") == 0) {
114 printf("m\n"); /* Needed for sqrt() used in example code above. */