forked from ocaml-community/yojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ydump.ml
165 lines (144 loc) · 4.46 KB
/
ydump.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
open Printf
let license = "\
Copyright (c) 2010-2012 Martin Jambon
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"
let polycat write_one streaming in_file out_file =
let ic, fname =
match in_file with
`Stdin -> stdin, "<stdin>"
| `File s -> open_in s, s
in
let oc =
match out_file with
`Stdout -> stdout
| `File s -> open_out s
in
let finally () =
if oc != stdout then
close_out_noerr oc;
if ic != stdin then
close_in_noerr ic
in
try
if streaming then
Stream.iter (write_one oc) (Yojson.Safe.stream_from_channel ~fname ic)
else
write_one oc (Yojson.Safe.from_channel ~fname ic);
finally ();
true
with e ->
finally ();
eprintf "Error:\n";
(match e with
Yojson.Json_error s ->
eprintf "%s\n%!" s
| e ->
eprintf "%s\n%!" (Printexc.to_string e)
);
false
let cat output_biniou std compact streaming in_file out_file =
if not output_biniou then
let write_one oc x =
if compact then
Yojson.Safe.to_channel ~std oc x
else
Yojson.Safe.pretty_to_channel ~std oc x;
output_char oc '\n'
in
polycat write_one streaming in_file out_file
else
let write_one oc x =
output_string oc (Bi_io.string_of_tree (Yojson_biniou.biniou_of_json x))
in
polycat write_one streaming in_file out_file
let parse_cmdline () =
let out = ref None in
let std = ref false in
let compact = ref false in
let streaming = ref true in
let output_biniou = ref false in
let options = [
"-o", Arg.String (fun s -> out := Some s),
"<file>
Output file";
"-std", Arg.Set std,
"
Convert tuples and variants into standard JSON,
refuse to print NaN and infinities,
require the root node to be either an object or an array.";
"-c", Arg.Set compact,
"
Compact output (default: pretty-printed)";
"-s", Arg.Set streaming,
"
Streaming mode: read and write a sequence of JSON values instead of
just one (default).";
"-u", Arg.Clear streaming,
"
A single JSON record is expected.
(no longer the default since 1.1.1)";
"-ob", Arg.Set output_biniou,
"\
Experimental";
"-version",
Arg.Unit (fun () -> print_endline Yojson.version; exit 0),
"\
Print version of yojson and ydump and exit."
]
in
let files = ref [] in
let anon_fun s =
files := s :: !files
in
let msg =
sprintf "\
JSON pretty-printer based on the Yojson library for OCaml
%s
JSON pretty-printer based on the Yojson library for OCaml
Usage: %s [input file]"
license Sys.argv.(0)
in
Arg.parse options anon_fun msg;
let in_file =
match List.rev !files with
[] -> `Stdin
| [x] -> `File x
| _ ->
eprintf "Too many input files\n%!";
exit 1
in
let out_file =
match !out with
None -> `Stdout
| Some x -> `File x
in
!output_biniou, !std, !compact, !streaming, in_file, out_file
let () =
let output_biniou, std, compact, streaming, in_file, out_file =
parse_cmdline () in
let success = cat output_biniou std compact streaming in_file out_file in
if success then
exit 0
else
exit 1