7a19091900573f656617c5ea699afac7c45eff7a
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_opcode
*tree
;
80 struct json_object
*res
;
81 const char *error
, *prefix
;
83 tree
= jp_parse(expr
, &error
);
87 fprintf(stderr
, "In expression '%s': %s\n", expr
, error
);
91 res
= jp_match(tree
, jsobj
);
93 if (tree
->type
== T_LABEL
)
97 switch (json_object_get_type(res
))
99 case json_type_object
:
100 printf("export %s_TYPE=object; ", prefix
);
103 printf("export %s_KEYS=", prefix
);
104 json_object_object_foreach(res
, key
, val
)
117 //printf("export %s=", prefix);
118 //print_string(json_object_to_json_string(res));
123 case json_type_array
:
124 printf("export %s_TYPE=array; ", prefix
);
125 printf("export %s_LENGTH=%d; ",
126 prefix
, json_object_array_length(res
));
128 //printf("export %s=", prefix);
129 //print_string(json_object_to_json_string(res));
133 case json_type_boolean
:
134 printf("export %s_TYPE=bool; ", prefix
);
135 printf("export %s=%d; ", prefix
, json_object_get_boolean(res
));
139 printf("export %s_TYPE=int; ", prefix
);
140 printf("export %s=%d; ", prefix
, json_object_get_int(res
));
143 case json_type_double
:
144 printf("export %s_TYPE=double; ", prefix
);
145 printf("export %s=%f; ", prefix
, json_object_get_double(res
));
148 case json_type_string
:
149 printf("export %s_TYPE=string; ", prefix
);
150 printf("export %s=", prefix
);
151 print_string(json_object_get_string(res
));
156 printf("unset %s %s_TYPE %s_LENGTH %s_KEYS; ",
157 prefix
, prefix
, prefix
, prefix
);
163 printf("%s\n", json_object_to_json_string(res
));
169 int main(int argc
, char **argv
)
173 struct json_object
*jsobj
= NULL
;
175 while ((opt
= getopt(argc
, argv
, "i:e:")) != -1)
180 input
= fopen(optarg
, "r");
184 fprintf(stderr
, "Failed to open %s: %s\n",
185 optarg
, strerror(errno
));
195 jsobj
= parse_json(input
);
199 fprintf(stderr
, "Failed to parse json data\n");
204 export_json(jsobj
, optarg
);
210 json_object_put(jsobj
);