]> Dogcows Code - chaz/yoink/blob - yajl/reformatter/json_reformat.c
minor cleanups
[chaz/yoink] / yajl / reformatter / json_reformat.c
1 /*
2 * Copyright 2007-2009, Lloyd Hilaiel.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. Neither the name of Lloyd Hilaiel nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <yajl/yajl_parse.h>
34 #include <yajl/yajl_gen.h>
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 static int reformat_null(void * ctx)
41 {
42 yajl_gen g = (yajl_gen) ctx;
43 yajl_gen_null(g);
44 return 1;
45 }
46
47 static int reformat_boolean(void * ctx, int boolean)
48 {
49 yajl_gen g = (yajl_gen) ctx;
50 yajl_gen_bool(g, boolean);
51 return 1;
52 }
53
54 static int reformat_number(void * ctx, const char * s, unsigned int l)
55 {
56 yajl_gen g = (yajl_gen) ctx;
57 yajl_gen_number(g, s, l);
58 return 1;
59 }
60
61 static int reformat_string(void * ctx, const unsigned char * stringVal,
62 unsigned int stringLen)
63 {
64 yajl_gen g = (yajl_gen) ctx;
65 yajl_gen_string(g, stringVal, stringLen);
66 return 1;
67 }
68
69 static int reformat_map_key(void * ctx, const unsigned char * stringVal,
70 unsigned int stringLen)
71 {
72 yajl_gen g = (yajl_gen) ctx;
73 yajl_gen_string(g, stringVal, stringLen);
74 return 1;
75 }
76
77 static int reformat_start_map(void * ctx)
78 {
79 yajl_gen g = (yajl_gen) ctx;
80 yajl_gen_map_open(g);
81 return 1;
82 }
83
84
85 static int reformat_end_map(void * ctx)
86 {
87 yajl_gen g = (yajl_gen) ctx;
88 yajl_gen_map_close(g);
89 return 1;
90 }
91
92 static int reformat_start_array(void * ctx)
93 {
94 yajl_gen g = (yajl_gen) ctx;
95 yajl_gen_array_open(g);
96 return 1;
97 }
98
99 static int reformat_end_array(void * ctx)
100 {
101 yajl_gen g = (yajl_gen) ctx;
102 yajl_gen_array_close(g);
103 return 1;
104 }
105
106 static yajl_callbacks callbacks = {
107 reformat_null,
108 reformat_boolean,
109 NULL,
110 NULL,
111 reformat_number,
112 reformat_string,
113 reformat_start_map,
114 reformat_map_key,
115 reformat_end_map,
116 reformat_start_array,
117 reformat_end_array
118 };
119
120 static void
121 usage(const char * progname)
122 {
123 fprintf(stderr, "usage: %s <filename>\n"
124 " -m minimize json rather than beautify (default)\n"
125 " -u allow invalid UTF8 inside strings during parsing\n",
126 progname);
127 exit(1);
128
129 }
130
131 int
132 main(int argc, char ** argv)
133 {
134 yajl_handle hand;
135 static unsigned char fileData[65536];
136 /* generator config */
137 yajl_gen_config conf = { 1, " " };
138 yajl_gen g;
139 yajl_status stat;
140 size_t rd;
141 /* allow comments */
142 yajl_parser_config cfg = { 1, 1 };
143 int done = 0;
144
145 /* check arguments. We expect exactly one! */
146 if (argc == 2) {
147 if (!strcmp("-m", argv[1])) {
148 conf.beautify = 0;
149
150 } else if (!strcmp("-u", argv[1])) {
151 cfg.checkUTF8 = 0;
152 } else {
153 usage(argv[0]);
154 }
155 } else if (argc != 1) {
156 usage(argv[0]);
157 }
158
159 g = yajl_gen_alloc(&conf, NULL);
160
161 /* ok. open file. let's read and parse */
162 hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g);
163
164 while (!done) {
165 rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
166
167 if (rd == 0) {
168 if (!feof(stdin)) {
169 fprintf(stderr, "error on file read.\n");
170 break;
171 }
172 done = 1;
173 }
174 fileData[rd] = 0;
175
176 if (done)
177 /* parse any remaining buffered data */
178 stat = yajl_parse_complete(hand);
179 else
180 /* read file data, pass to parser */
181 stat = yajl_parse(hand, fileData, rd);
182
183 if (stat != yajl_status_ok &&
184 stat != yajl_status_insufficient_data)
185 {
186 unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
187 fprintf(stderr, (const char *) str);
188 yajl_free_error(hand, str);
189 } else {
190 const unsigned char * buf;
191 unsigned int len;
192 yajl_gen_get_buf(g, &buf, &len);
193 fwrite(buf, 1, len, stdout);
194 yajl_gen_clear(g);
195 }
196 }
197
198 yajl_gen_free(g);
199 yajl_free(hand);
200
201 return 0;
202 }
This page took 0.038504 seconds and 4 git commands to generate.