2 * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
22 static int getenv_int(const char *name)
31 result = strtol(value, &end, 10);
38 /* Return a pointer to the first non-whitespace character of str.
39 Modifies str so that all trailing whitespace characters are
41 static const char *strip(char *str)
45 while(*result && isspace(*result))
48 length = strlen(result);
52 while(isspace(result[length - 1]))
53 result[--length] = '\0';
58 int main(int argc, char *argv[])
67 setlocale(LC_ALL, "");
71 fprintf(stderr, "usage: %s\n", argv[0]);
75 indent = getenv_int("JSON_INDENT");
76 if(indent < 0 || indent > 255) {
77 fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
82 flags |= JSON_INDENT(indent);
84 if(getenv_int("JSON_COMPACT") > 0)
85 flags |= JSON_COMPACT;
87 if(getenv_int("JSON_ENSURE_ASCII"))
88 flags |= JSON_ENSURE_ASCII;
90 if(getenv_int("JSON_PRESERVE_ORDER"))
91 flags |= JSON_PRESERVE_ORDER;
93 if(getenv_int("JSON_SORT_KEYS"))
94 flags |= JSON_SORT_KEYS;
96 if(getenv_int("STRIP")) {
97 /* Load to memory, strip leading and trailing whitespace */
98 size_t size = 0, used = 0;
104 size = (size == 0 ? 128 : size * 2);
105 buffer = realloc(buffer, size);
107 fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
111 count = fread(buffer + used, 1, size - used, stdin);
112 if(count < size - used) {
113 buffer[used + count] = '\0';
119 json = json_loads(strip(buffer), 0, &error);
123 json = json_loadf(stdin, 0, &error);
126 fprintf(stderr, "%d %d %d\n%s\n",
127 error.line, error.column, error.position,
132 json_dumpf(json, stdout, flags);