"Fossies" - the Fresh Open Source Software Archive 
Member "jansson-2.14/examples/simple_parse.c" (19 Nov 2020, 5378 Bytes) of package /linux/www/jansson-2.14.tar.bz2:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * Simple example of parsing and printing JSON using jansson.
3 *
4 * SYNOPSIS:
5 * $ examples/simple_parse
6 * Type some JSON > [true, false, null, 1, 0.0, -0.0, "", {"name": "barney"}]
7 * JSON Array of 8 elements:
8 * JSON True
9 * JSON False
10 * JSON Null
11 * JSON Integer: "1"
12 * JSON Real: 0.000000
13 * JSON Real: -0.000000
14 * JSON String: ""
15 * JSON Object of 1 pair:
16 * JSON Key: "name"
17 * JSON String: "barney"
18 *
19 * Copyright (c) 2014 Robert Poor <rdpoor@gmail.com>
20 *
21 * Jansson is free software; you can redistribute it and/or modify
22 * it under the terms of the MIT license. See LICENSE for details.
23 */
24
25 #include <jansson.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 /* forward refs */
30 void print_json(json_t *root);
31 void print_json_aux(json_t *element, int indent);
32 void print_json_indent(int indent);
33 const char *json_plural(size_t count);
34 void print_json_object(json_t *element, int indent);
35 void print_json_array(json_t *element, int indent);
36 void print_json_string(json_t *element, int indent);
37 void print_json_integer(json_t *element, int indent);
38 void print_json_real(json_t *element, int indent);
39 void print_json_true(json_t *element, int indent);
40 void print_json_false(json_t *element, int indent);
41 void print_json_null(json_t *element, int indent);
42
43 void print_json(json_t *root) { print_json_aux(root, 0); }
44
45 void print_json_aux(json_t *element, int indent) {
46 switch (json_typeof(element)) {
47 case JSON_OBJECT:
48 print_json_object(element, indent);
49 break;
50 case JSON_ARRAY:
51 print_json_array(element, indent);
52 break;
53 case JSON_STRING:
54 print_json_string(element, indent);
55 break;
56 case JSON_INTEGER:
57 print_json_integer(element, indent);
58 break;
59 case JSON_REAL:
60 print_json_real(element, indent);
61 break;
62 case JSON_TRUE:
63 print_json_true(element, indent);
64 break;
65 case JSON_FALSE:
66 print_json_false(element, indent);
67 break;
68 case JSON_NULL:
69 print_json_null(element, indent);
70 break;
71 default:
72 fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element));
73 }
74 }
75
76 void print_json_indent(int indent) {
77 int i;
78 for (i = 0; i < indent; i++) {
79 putchar(' ');
80 }
81 }
82
83 const char *json_plural(size_t count) { return count == 1 ? "" : "s"; }
84
85 void print_json_object(json_t *element, int indent) {
86 size_t size;
87 const char *key;
88 json_t *value;
89
90 print_json_indent(indent);
91 size = json_object_size(element);
92
93 printf("JSON Object of %lld pair%s:\n", (long long)size, json_plural(size));
94 json_object_foreach(element, key, value) {
95 print_json_indent(indent + 2);
96 printf("JSON Key: \"%s\"\n", key);
97 print_json_aux(value, indent + 2);
98 }
99 }
100
101 void print_json_array(json_t *element, int indent) {
102 size_t i;
103 size_t size = json_array_size(element);
104 print_json_indent(indent);
105
106 printf("JSON Array of %lld element%s:\n", (long long)size, json_plural(size));
107 for (i = 0; i < size; i++) {
108 print_json_aux(json_array_get(element, i), indent + 2);
109 }
110 }
111
112 void print_json_string(json_t *element, int indent) {
113 print_json_indent(indent);
114 printf("JSON String: \"%s\"\n", json_string_value(element));
115 }
116
117 void print_json_integer(json_t *element, int indent) {
118 print_json_indent(indent);
119 printf("JSON Integer: \"%" JSON_INTEGER_FORMAT "\"\n", json_integer_value(element));
120 }
121
122 void print_json_real(json_t *element, int indent) {
123 print_json_indent(indent);
124 printf("JSON Real: %f\n", json_real_value(element));
125 }
126
127 void print_json_true(json_t *element, int indent) {
128 (void)element;
129 print_json_indent(indent);
130 printf("JSON True\n");
131 }
132
133 void print_json_false(json_t *element, int indent) {
134 (void)element;
135 print_json_indent(indent);
136 printf("JSON False\n");
137 }
138
139 void print_json_null(json_t *element, int indent) {
140 (void)element;
141 print_json_indent(indent);
142 printf("JSON Null\n");
143 }
144
145 /*
146 * Parse text into a JSON object. If text is valid JSON, returns a
147 * json_t structure, otherwise prints and error and returns null.
148 */
149 json_t *load_json(const char *text) {
150 json_t *root;
151 json_error_t error;
152
153 root = json_loads(text, 0, &error);
154
155 if (root) {
156 return root;
157 } else {
158 fprintf(stderr, "json error on line %d: %s\n", error.line, error.text);
159 return (json_t *)0;
160 }
161 }
162
163 /*
164 * Print a prompt and return (by reference) a null-terminated line of
165 * text. Returns NULL on eof or some error.
166 */
167 char *read_line(char *line, int max_chars) {
168 printf("Type some JSON > ");
169 fflush(stdout);
170 return fgets(line, max_chars, stdin);
171 }
172
173 /* ================================================================
174 * main
175 */
176
177 #define MAX_CHARS 4096
178
179 int main(int argc, char *argv[]) {
180 char line[MAX_CHARS];
181
182 if (argc != 1) {
183 fprintf(stderr, "Usage: %s\n", argv[0]);
184 exit(-1);
185 }
186
187 while (read_line(line, MAX_CHARS) != (char *)NULL) {
188
189 /* parse text into JSON structure */
190 json_t *root = load_json(line);
191
192 if (root) {
193 /* print and release the JSON structure */
194 print_json(root);
195 json_decref(root);
196 }
197 }
198
199 return 0;
200 }