2 * Copyright 2007-2009, Lloyd Hilaiel.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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.
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.
33 #include <yajl/yajl_parse.h>
34 #include <yajl/yajl_gen.h>
40 static int reformat_null(void * ctx
)
42 yajl_gen g
= (yajl_gen
) ctx
;
47 static int reformat_boolean(void * ctx
, int boolean
)
49 yajl_gen g
= (yajl_gen
) ctx
;
50 yajl_gen_bool(g
, boolean
);
54 static int reformat_number(void * ctx
, const char * s
, unsigned int l
)
56 yajl_gen g
= (yajl_gen
) ctx
;
57 yajl_gen_number(g
, s
, l
);
61 static int reformat_string(void * ctx
, const unsigned char * stringVal
,
62 unsigned int stringLen
)
64 yajl_gen g
= (yajl_gen
) ctx
;
65 yajl_gen_string(g
, stringVal
, stringLen
);
69 static int reformat_map_key(void * ctx
, const unsigned char * stringVal
,
70 unsigned int stringLen
)
72 yajl_gen g
= (yajl_gen
) ctx
;
73 yajl_gen_string(g
, stringVal
, stringLen
);
77 static int reformat_start_map(void * ctx
)
79 yajl_gen g
= (yajl_gen
) ctx
;
85 static int reformat_end_map(void * ctx
)
87 yajl_gen g
= (yajl_gen
) ctx
;
88 yajl_gen_map_close(g
);
92 static int reformat_start_array(void * ctx
)
94 yajl_gen g
= (yajl_gen
) ctx
;
95 yajl_gen_array_open(g
);
99 static int reformat_end_array(void * ctx
)
101 yajl_gen g
= (yajl_gen
) ctx
;
102 yajl_gen_array_close(g
);
106 static yajl_callbacks callbacks
= {
116 reformat_start_array
,
121 usage(const char * progname
)
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",
132 main(int argc
, char ** argv
)
135 static unsigned char fileData
[65536];
136 /* generator config */
137 yajl_gen_config conf
= { 1, " " };
142 yajl_parser_config cfg
= { 1, 1 };
145 /* check arguments. We expect exactly one! */
147 if (!strcmp("-m", argv
[1])) {
150 } else if (!strcmp("-u", argv
[1])) {
155 } else if (argc
!= 1) {
159 g
= yajl_gen_alloc(&conf
, NULL
);
161 /* ok. open file. let's read and parse */
162 hand
= yajl_alloc(&callbacks
, &cfg
, NULL
, (void *) g
);
165 rd
= fread((void *) fileData
, 1, sizeof(fileData
) - 1, stdin
);
169 fprintf(stderr
, "error on file read.\n");
177 /* parse any remaining buffered data */
178 stat
= yajl_parse_complete(hand
);
180 /* read file data, pass to parser */
181 stat
= yajl_parse(hand
, fileData
, rd
);
183 if (stat
!= yajl_status_ok
&&
184 stat
!= yajl_status_insufficient_data
)
186 unsigned char * str
= yajl_get_error(hand
, 1, fileData
, rd
);
187 fprintf(stderr
, (const char *) str
);
188 yajl_free_error(hand
, str
);
190 const unsigned char * buf
;
192 yajl_gen_get_buf(g
, &buf
, &len
);
193 fwrite(buf
, 1, len
, stdout
);