]> git.ozlabs.org Git - ccan/blob - ccan/json/_info
0544cf5bb84c4b2d0e9a1efd919e22dc65f74484
[ccan] / ccan / json / _info
1 #include <string.h>
2 #include "config.h"
3
4 /**
5  * json - Parse and generate JSON (JavaScript Object Notation)
6  *
7  * This is a library for encoding and decoding JSON that strives to be
8  * easy to learn, use, and incorporate into an application.
9  *
10  * JSON (JavaScript Object Notation) facilitates passing data among different
11  * programming languages, particularly JavaScript.  It looks like this:
12  *
13  *     [
14  *         {
15  *             "id":           1,
16  *             "firstname":    "John",
17  *             "lastname":     "Smith",
18  *             "email":        "john@example.com",
19  *             "likes_pizza":  false
20  *         },
21  *         {
22  *             "id":           2,
23  *             "firstname":    "Linda",
24  *             "lastname":     "Jones",
25  *             "email":        null,
26  *             "likes_pizza":  true
27  *         }
28  *     ]
29  *
30  * Example:
31  *      #include <ccan/json/json.h>
32  *      #include <math.h>
33  *      #include <stdio.h>
34  *      #include <stdlib.h>
35  *      
36  *      static int find_number(JsonNode *object, const char *name, double *out)
37  *      {
38  *              JsonNode *node = json_find_member(object, name);
39  *              if (node && node->tag == JSON_NUMBER) {
40  *                      *out = node->number_;
41  *                      return 1;
42  *              }
43  *              return 0;
44  *      }
45  *      
46  *      static void solve_pythagorean(JsonNode *triple)
47  *      {
48  *              double a = 0, b = 0, c = 0;
49  *              int a_given, b_given, c_given;
50  *              
51  *              if (triple->tag != JSON_OBJECT) {
52  *                      fprintf(stderr, "Error: Expected a JSON object.\n");
53  *                      exit(EXIT_FAILURE);
54  *              }
55  *              
56  *              a_given = find_number(triple, "a", &a);
57  *              b_given = find_number(triple, "b", &b);
58  *              c_given = find_number(triple, "c", &c);
59  *              
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");
62  *                      exit(EXIT_FAILURE);
63  *              }
64  *              
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));
74  *              }
75  *      }
76  *      
77  *      int main(void)
78  *      {
79  *              JsonNode *triples = json_mkarray();
80  *              
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}"));
84  *              
85  *              JsonNode *triple;
86  *              json_foreach(triple, triples)
87  *                      solve_pythagorean(triple);
88  *              
89  *              char *tmp = json_stringify(triples, "\t");
90  *              puts(tmp);
91  *              free(tmp);
92  *              
93  *              json_delete(triples);
94  *              return 0;
95  *      }
96  *
97  * Author: Joey Adams
98  * Version: 0.1
99  * License: MIT
100  */
101 int main(int argc, char *argv[])
102 {
103         /* Expect exactly one argument */
104         if (argc != 2)
105                 return 1;
106
107         if (strcmp(argv[1], "depends") == 0) {
108                 /* Nothing */
109                 return 0;
110         }
111         
112         if (strcmp(argv[1], "libs") == 0) {
113                 printf("m\n"); /* Needed for sqrt() used in example code above. */
114                 return 0;
115         }
116
117         return 1;
118 }