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.
14 static int getenv_int(const char *name)
23 result = strtol(value, &end, 10);
30 /* Return a pointer to the first non-whitespace character of str.
31 Modifies str so that all trailing whitespace characters are
33 static const char *strip(char *str)
37 while(*result && isspace(*result))
40 length = strlen(result);
44 while(isspace(result[length - 1]))
45 result[--length] = '\0';
50 int main(int argc, char *argv[])
59 fprintf(stderr, "usage: %s\n", argv[0]);
63 indent = getenv_int("JSON_INDENT");
64 if(indent < 0 || indent > 255) {
65 fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
70 flags |= JSON_INDENT(indent);
72 if(getenv_int("JSON_COMPACT") > 0)
73 flags |= JSON_COMPACT;
75 if(getenv_int("JSON_ENSURE_ASCII"))
76 flags |= JSON_ENSURE_ASCII;
78 if(getenv_int("JSON_PRESERVE_ORDER"))
79 flags |= JSON_PRESERVE_ORDER;
81 if(getenv_int("JSON_SORT_KEYS"))
82 flags |= JSON_SORT_KEYS;
84 if(getenv_int("STRIP")) {
85 /* Load to memory, strip leading and trailing whitespace */
86 size_t size = 0, used = 0;
92 size = (size == 0 ? 128 : size * 2);
93 buffer = realloc(buffer, size);
95 fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
99 count = fread(buffer + used, 1, size - used, stdin);
100 if(count < size - used) {
101 buffer[used + count] = '\0';
107 json = json_loads(strip(buffer), 0, &error);
111 json = json_loadf(stdin, 0, &error);
114 fprintf(stderr, "%d %d %d\n%s\n",
115 error.line, error.column, error.position,
120 json_dumpf(json, stdout, flags);