X-Git-Url: http://git.archive.openwrt.org/?p=project%2Flibubox.git;a=blobdiff_plain;f=jshn.c;h=e7e02d19301f12fa158cb5a3bbe286e5d0850fd6;hp=9cbdb3194489d180406c8f656fa98d5c014c82aa;hb=213122a0830e1ba3a05013b5cb5d17c49af13282;hpb=0ab17bcb3aac5ed4234d43d170208adedc9cec71 diff --git a/jshn.c b/jshn.c index 9cbdb31..e7e02d1 100644 --- a/jshn.c +++ b/jshn.c @@ -13,7 +13,12 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include +#ifdef JSONC + #include +#else + #include +#endif + #include #include #include @@ -22,8 +27,13 @@ #include #include "list.h" +#include "blob.h" +#include "blobmsg_json.h" + #define MAX_VARLEN 256 +static struct blob_buf b = { 0 }; + static const char *var_prefix = ""; static int var_prefix_len = 0; @@ -107,6 +117,9 @@ static int add_json_element(const char *key, json_object *obj) case json_type_int: type = "int"; break; + case json_type_double: + type = "double"; + break; default: return -1; } @@ -136,6 +149,9 @@ static int add_json_element(const char *key, json_object *obj) case json_type_int: fprintf(stdout, "' %d;\n", json_object_get_int(obj)); break; + case json_type_double: + fprintf(stdout, "' %lf;\n", json_object_get_double(obj)); + break; default: return -1; } @@ -148,7 +164,7 @@ static int jshn_parse(const char *str) json_object *obj; obj = json_tokener_parse(str); - if (is_error(obj) || json_object_get_type(obj) != json_type_object) { + if (!obj || json_object_get_type(obj) != json_type_object) { fprintf(stderr, "Failed to parse message data\n"); return 1; } @@ -207,6 +223,8 @@ static void jshn_add_object_var(json_object *obj, bool array, const char *prefix new = json_object_new_string(var); } else if (!strcmp(type, "int")) { new = json_object_new_int(atoi(var)); + } else if (!strcmp(type, "double")) { + new = json_object_new_double(strtod(var, NULL)); } else if (!strcmp(type, "boolean")) { new = json_object_new_boolean(!!atoi(var)); } else { @@ -236,30 +254,37 @@ out: return obj; } -static int jshn_format(bool no_newline) +static int jshn_format(bool no_newline, bool indent) { json_object *obj; + const char *output; obj = json_object_new_object(); jshn_add_objects(obj, "JSON_VAR", false); - fprintf(stdout, "%s%s", json_object_to_json_string(obj), - no_newline ? "" : "\n"); + output = json_object_to_json_string(obj); + if (indent) { + blob_buf_init(&b, 0); + blobmsg_add_json_from_string(&b, output); + output = blobmsg_format_json_indent(b.head, 1, 0); + } + fprintf(stdout, "%s%s", output, no_newline ? "" : "\n"); json_object_put(obj); return 0; } static int usage(const char *progname) { - fprintf(stderr, "Usage: %s [-n] -r |-w\n", progname); + fprintf(stderr, "Usage: %s [-n] [-i] -r |-w\n", progname); return 2; } int main(int argc, char **argv) { bool no_newline = false; + bool indent = false; int ch; - while ((ch = getopt(argc, argv, "p:nr:w")) != -1) { + while ((ch = getopt(argc, argv, "p:nir:w")) != -1) { switch(ch) { case 'p': var_prefix = optarg; @@ -268,10 +293,13 @@ int main(int argc, char **argv) case 'r': return jshn_parse(optarg); case 'w': - return jshn_format(no_newline); + return jshn_format(no_newline, indent); case 'n': no_newline = true; break; + case 'i': + indent = true; + break; default: return usage(argv[0]); }