forked from fuzziqersoftware/phosg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONFormat.cc
111 lines (97 loc) · 2.97 KB
/
JSONFormat.cc
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include "Filesystem.hh"
#include "JSON.hh"
#include "JSONPickle.hh"
using namespace std;
void print_usage(const char* argv0) {
fprintf(stderr, "\
Usage: %s [options] infile outfile\n\
\n\
If infile is - or not specified, read from standard input.\n\
If outfile is - or not specified, write to standard output.\n\
\n\
Options:\n\
--format: Write output JSON in a human-readable format (default).\n\
--compress: Instead of formatting in a human-readable format, minimize the\n\
size of the resulting data. No effect when --write-pickle is specified.\n\
--read-pickle: Expect input data in pickle format instead of JSON.\n\
--write-pickle: Write output in pickle format (protocol 2) instead of JSON.\n\
--binary-stdout: Don\'t complain if writing pickle data to standard output\n\
when it\'s a terminal.\n\
\n", argv0);
}
int main(int argc, char** argv) {
bool format = true;
bool write_pickle = false;
bool read_pickle = false;
bool binary_stdout = false;
const char* src_filename = NULL;
const char* dst_filename = NULL;
for (int x = 1; x < argc; x++) {
if (argv[x][0] == '-') {
if (!strcmp(argv[x], "--help")) {
print_usage(argv[0]);
return 1;
} else if (!strcmp(argv[x], "--format")) {
format = true;
} else if (!strcmp(argv[x], "--compress")) {
format = false;
} else if (!strcmp(argv[x], "--read-pickle")) {
read_pickle = true;
} else if (!strcmp(argv[x], "--write-pickle")) {
write_pickle = true;
} else if (!strcmp(argv[x], "--binary-stdout")) {
binary_stdout = true;
} else {
fprintf(stderr, "unknown argument: %s\n", argv[x]);
return 1;
}
} else if (!src_filename) {
src_filename = argv[x];
} else if (!dst_filename) {
dst_filename = argv[x];
} else {
fprintf(stderr, "too many positional arguments given\n");
return 1;
}
}
if (write_pickle && !binary_stdout && isatty(fileno(stdout))) {
fprintf(stderr, "stdout is a terminal; writing pickle data will likely be unreadable\n");
fprintf(stderr, "use --binary-stdout to write pickle data to stdout anyway\n");
return 1;
}
string src_data;
if (!src_filename || !strcmp(src_filename, "-")) {
src_data = read_all(stdin);
} else {
src_data = load_file(src_filename);
}
shared_ptr<JSONObject> json;
try {
if (read_pickle) {
json = parse_pickle(src_data);
} else {
json = JSONObject::parse(src_data);
}
} catch (const exception& e) {
fprintf(stderr, "cannot parse input: %s\n", e.what());
return 2;
}
string result;
if (write_pickle) {
result = serialize_pickle(*json);
} else if (format) {
result = json->format();
} else {
result = json->serialize();
}
if (!dst_filename || !strcmp(dst_filename, "-")) {
fwritex(stdout, result);
} else {
save_file(dst_filename, result);
}
return 0;
}