summaryrefslogtreecommitdiffstats
path: root/contrib/ccan/json/_info
blob: 3113b63ee7e5f42b4ccaeeafe805ca58e793fc5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "config.h"
#include <stdio.h>
#include <string.h>

/**
 * json - Parse and generate JSON (JavaScript Object Notation)
 *
 * This is a library for encoding and decoding JSON that strives to be
 * easy to learn, use, and incorporate into an application.
 *
 * JSON (JavaScript Object Notation) facilitates passing data among different
 * programming languages, particularly JavaScript.  It looks like this:
 *
 *     [
 *         {
 *             "id":           1,
 *             "firstname":    "John",
 *             "lastname":     "Smith",
 *             "email":        "john@example.com",
 *             "likes_pizza":  false
 *         },
 *         {
 *             "id":           2,
 *             "firstname":    "Linda",
 *             "lastname":     "Jones",
 *             "email":        null,
 *             "likes_pizza":  true
 *         }
 *     ]
 *
 * Example:
 *	#include <ccan/json/json.h>
 *	#include <math.h>
 *	#include <stdio.h>
 *	#include <stdlib.h>
 *	
 *	static int find_number(JsonNode *object, const char *name, double *out)
 *	{
 *		JsonNode *node = json_find_member(object, name);
 *		if (node && node->tag == JSON_NUMBER) {
 *			*out = node->number_;
 *			return 1;
 *		}
 *		return 0;
 *	}
 *	
 *	static void solve_pythagorean(JsonNode *triple)
 *	{
 *		double a = 0, b = 0, c = 0;
 *		int a_given, b_given, c_given;
 *		
 *		if (triple->tag != JSON_OBJECT) {
 *			fprintf(stderr, "Error: Expected a JSON object.\n");
 *			exit(EXIT_FAILURE);
 *		}
 *		
 *		a_given = find_number(triple, "a", &a);
 *		b_given = find_number(triple, "b", &b);
 *		c_given = find_number(triple, "c", &c);
 *		
 *		if (a_given + b_given + c_given != 2) {
 *			fprintf(stderr, "Error: I need two sides to compute the length of the third.\n");
 *			exit(EXIT_FAILURE);
 *		}
 *		
 *		if (a_given && b_given) {
 *			c = sqrt(a*a + b*b);
 *			json_append_member(triple, "c", json_mknumber(c));
 *		} else if (a_given && c_given) {
 *			b = sqrt(c*c - a*a);
 *			json_append_member(triple, "b", json_mknumber(b));
 *		} else if (b_given && c_given) {
 *			a = sqrt(c*c - b*b);
 *			json_append_member(triple, "a", json_mknumber(a));
 *		}
 *	}
 *	
 *	int main(void)
 *	{
 *		JsonNode *triples = json_mkarray();
 *		
 *		json_append_element(triples, json_decode("{\"a\": 3, \"b\": 4}"));
 *		json_append_element(triples, json_decode("{\"a\": 5, \"c\": 13}"));
 *		json_append_element(triples, json_decode("{\"b\": 24, \"c\": 25}"));
 *		
 *		JsonNode *triple;
 *		json_foreach(triple, triples)
 *			solve_pythagorean(triple);
 *		
 *		char *tmp = json_stringify(triples, "\t");
 *		puts(tmp);
 *		free(tmp);
 *		
 *		json_delete(triples);
 *		return 0;
 *	}
 *
 * Author: Joey Adams
 * Version: 0.1
 * License: MIT
 */
int main(int argc, char *argv[])
{
	/* Expect exactly one argument */
	if (argc != 2)
		return 1;

	if (strcmp(argv[1], "depends") == 0) {
		/* Nothing */
		return 0;
	}
	
	if (strcmp(argv[1], "libs") == 0) {
		printf("m\n"); /* Needed for sqrt() used in example code above. */
		return 0;
	}

	return 1;
}