2 * Copyright (c) 2009-2012 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.
23 #include <io.h> /* for _setmode() */
24 #include <fcntl.h> /* for _O_BINARY */
27 static int getenv_int(const char *name)
36 result = strtol(value, &end, 10);
43 /* Return a pointer to the first non-whitespace character of str.
44 Modifies str so that all trailing whitespace characters are
46 static const char *strip(char *str)
50 while(*result && isspace(*result))
53 length = strlen(result);
57 while(isspace(result[length - 1]))
58 result[--length] = '\0';
63 int main(int argc, char *argv[])
72 setlocale(LC_ALL, "");
76 fprintf(stderr, "usage: %s\n", argv[0]);
81 /* On Windows, set stdout and stderr to binary mode to avoid
82 outputting DOS line terminators */
83 _setmode(_fileno(stdout), _O_BINARY);
84 _setmode(_fileno(stderr), _O_BINARY);
87 indent = getenv_int("JSON_INDENT");
88 if(indent < 0 || indent > 255) {
89 fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
94 flags |= JSON_INDENT(indent);
96 if(getenv_int("JSON_COMPACT") > 0)
97 flags |= JSON_COMPACT;
99 if(getenv_int("JSON_ENSURE_ASCII"))
100 flags |= JSON_ENSURE_ASCII;
102 if(getenv_int("JSON_PRESERVE_ORDER"))
103 flags |= JSON_PRESERVE_ORDER;
105 if(getenv_int("JSON_SORT_KEYS"))
106 flags |= JSON_SORT_KEYS;
108 if(getenv_int("STRIP")) {
109 /* Load to memory, strip leading and trailing whitespace */
110 size_t size = 0, used = 0;
116 size = (size == 0 ? 128 : size * 2);
117 buffer = realloc(buffer, size);
119 fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
123 count = fread(buffer + used, 1, size - used, stdin);
124 if(count < size - used) {
125 buffer[used + count] = '\0';
131 json = json_loads(strip(buffer), 0, &error);
135 json = json_loadf(stdin, 0, &error);
138 fprintf(stderr, "%d %d %d\n%s\n",
139 error.line, error.column, error.position,
144 json_dumpf(json, stdout, flags);