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