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