2 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include <json-c/json.h>
32 static struct json_object *
33 parse_json(FILE *fd, const char *source, const char **error)
37 struct json_object *obj = NULL;
38 struct json_tokener *tok = json_tokener_new();
39 enum json_tokener_error err = json_tokener_continue;
46 obj = json_tokener_parse_ex(tok, source, strlen(source));
47 err = json_tokener_get_error(tok);
51 while ((len = fread(buf, 1, sizeof(buf), fd)) > 0)
53 obj = json_tokener_parse_ex(tok, buf, len);
54 err = json_tokener_get_error(tok);
56 if (!err || err != json_tokener_continue)
61 json_tokener_free(tok);
65 if (err == json_tokener_continue)
66 err = json_tokener_error_parse_eof;
68 *error = json_tokener_error_desc(err);
76 print_string(const char *s)
94 export_value(struct json_object *jsobj, const char *prefix)
101 switch (json_object_get_type(jsobj))
103 case json_type_object:
104 printf("export %s=", prefix);
105 json_object_object_foreach(jsobj, key, val)
119 case json_type_array:
120 printf("export %s=", prefix);
121 for (n = 0, len = json_object_array_length(jsobj); n < len; n++)
132 case json_type_boolean:
133 printf("export %s=%d; ", prefix, json_object_get_boolean(jsobj));
137 printf("export %s=%d; ", prefix, json_object_get_int(jsobj));
140 case json_type_double:
141 printf("export %s=%f; ", prefix, json_object_get_double(jsobj));
144 case json_type_string:
145 printf("export %s=", prefix);
146 print_string(json_object_get_string(jsobj));
156 printf("%s\n", json_object_to_json_string(jsobj));
161 export_type(struct json_object *jsobj, const char *prefix)
163 const char *types[] = {
174 printf("export %s=%s; ", prefix, types[json_object_get_type(jsobj)]);
176 printf("%s\n", types[json_object_get_type(jsobj)]);
180 filter_json(int opt, struct json_object *jsobj, char *expr)
182 struct jp_state *state;
183 struct json_object *res = NULL;
184 const char *prefix = NULL;
186 state = jp_parse(expr);
188 if (!state || state->error)
190 fprintf(stderr, "In expression '%s': %s\n",
191 expr, state ? state->error : "Out of memory");
196 res = jp_match(state->path, jsobj);
200 prefix = (state->path->type == T_LABEL) ? state->path->str : NULL;
205 export_type(res, prefix);
209 export_value(res, prefix);
221 int main(int argc, char **argv)
225 struct json_object *jsobj = NULL;
226 const char *jserr = NULL, *source = NULL;
228 while ((opt = getopt(argc, argv, "i:s:e:t:q")) != -1)
233 input = fopen(optarg, "r");
237 fprintf(stderr, "Failed to open %s: %s\n",
238 optarg, strerror(errno));
254 jsobj = parse_json(input, source, &jserr);
258 fprintf(stderr, "Failed to parse json data: %s\n",
266 if (!filter_json(opt, jsobj, optarg))
279 json_object_put(jsobj);