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.
24 #include <json-c/json.h>
31 static struct json_object *
36 struct json_object *obj = NULL;
37 struct json_tokener *tok = json_tokener_new();
38 enum json_tokener_error err = json_tokener_continue;
43 while ((len = fread(buf, 1, sizeof(buf), fd)) > 0)
45 obj = json_tokener_parse_ex(tok, buf, len);
46 err = json_tokener_get_error(tok);
48 if (!err || err != json_tokener_continue)
52 json_tokener_free(tok);
54 return err ? NULL : obj;
58 print_string(const char *s)
76 export_json(struct json_object *jsobj, char *expr)
79 struct jp_state *state;
80 struct json_object *res;
83 state = jp_parse(expr);
85 if (!state || state->error)
87 fprintf(stderr, "In expression '%s': %s\n",
88 expr, state ? state->error : "Out of memory");
93 res = jp_match(state->path, jsobj);
95 if (state->path->type == T_LABEL)
97 prefix = state->path->str;
99 switch (json_object_get_type(res))
101 case json_type_object:
102 printf("export %s_TYPE=object; ", prefix);
105 printf("export %s_KEYS=", prefix);
106 json_object_object_foreach(res, key, val)
119 //printf("export %s=", prefix);
120 //print_string(json_object_to_json_string(res));
125 case json_type_array:
126 printf("export %s_TYPE=array; ", prefix);
127 printf("export %s_LENGTH=%d; ",
128 prefix, json_object_array_length(res));
130 //printf("export %s=", prefix);
131 //print_string(json_object_to_json_string(res));
135 case json_type_boolean:
136 printf("export %s_TYPE=bool; ", prefix);
137 printf("export %s=%d; ", prefix, json_object_get_boolean(res));
141 printf("export %s_TYPE=int; ", prefix);
142 printf("export %s=%d; ", prefix, json_object_get_int(res));
145 case json_type_double:
146 printf("export %s_TYPE=double; ", prefix);
147 printf("export %s=%f; ", prefix, json_object_get_double(res));
150 case json_type_string:
151 printf("export %s_TYPE=string; ", prefix);
152 printf("export %s=", prefix);
153 print_string(json_object_get_string(res));
158 printf("unset %s %s_TYPE %s_LENGTH %s_KEYS; ",
159 prefix, prefix, prefix, prefix);
165 printf("%s\n", json_object_to_json_string(res));
173 int main(int argc, char **argv)
177 struct json_object *jsobj = NULL;
179 while ((opt = getopt(argc, argv, "i:e:")) != -1)
184 input = fopen(optarg, "r");
188 fprintf(stderr, "Failed to open %s: %s\n",
189 optarg, strerror(errno));
199 jsobj = parse_json(input);
203 fprintf(stderr, "Failed to parse json data\n");
208 export_json(jsobj, optarg);
214 json_object_put(jsobj);