-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pretty.cpp
152 lines (121 loc) · 2.94 KB
/
Pretty.cpp
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
#include <fstream>
#include <iostream>
#include <sstream>
#include "jparser/jparser.h"
using namespace std;
using namespace jparser;
class PrettyVisitor : public ConstVisitor {
public:
PrettyVisitor(int nspaces = 4, char space = ' ') {
indent_n_ = nspaces;
indent_char_ = space;
}
void *Visit(const Document& document) override {
if (document.document) {
return document.document->Accept(*this);
}
return nullptr;
}
void *Visit(const StreamDocument& document) override {
if (document.values.size() > 0) {
document.values[0]->Accept(*this);
}
for (size_t i = 1; i < document.values.size(); i++) {
partial_ += "\n";
document.values[i]->Accept(*this);
}
return nullptr;
}
void *Visit(const Object& object) override {
if (object.members.size() == 0) {
partial_ += "{}";
return nullptr;
}
partial_ += "{\n";
indentation_++;
Indent();
object.members[0]->Accept(*this);
for (size_t i = 1; i < object.members.size(); i++) {
partial_ += ",\n";
Indent();
object.members[i]->Accept(*this);
}
indentation_--;
partial_ += "\n";
Indent();
partial_ += "}";
return nullptr;
}
void *Visit(const Array& array) override {
if (array.values.size() == 0) {
partial_ += "[]";
return nullptr;
}
partial_ += "[\n";
indentation_++;
Indent();
array.values[0]->Accept(*this);
for (size_t i = 1; i < array.values.size(); i++) {
partial_ += ",\n";
Indent();
array.values[i]->Accept(*this);
}
indentation_--;
partial_ += "\n";
Indent();
partial_ += "]";
return nullptr;
}
void *Visit(const Member& member) override {
if (member.name) {
member.name->Accept(*this);
}
if (member.value) {
partial_ += ": ";
member.value->Accept(*this);
}
return nullptr;
}
void *Visit(const Name& name) override {
partial_ += "\"" + name.text + "\"";
return nullptr;
}
void *Visit(const Value& value) override {
return value.value->Accept(*this);
}
void *Visit(const Literal& literal) override {
partial_ += literal.text;
return nullptr;
}
string GetResult() const {
return partial_;
}
protected:
void Indent() {
partial_ += string(indentation_ * indent_n_, indent_char_);
}
char indent_char_ = ' ';
int indent_n_ = 4;
int indentation_ = 0;
string partial_;
};
int main(int argc, char **argv) {
stringstream input;
if (argc > 1) {
ifstream ifs(argv[1]);
input << ifs.rdbuf();
} else {
input << cin.rdbuf();
}
StreamDocument document;
if (!document.From(input.str())) {
cerr << "\e[0;31mERROR \e[0m: input text is not in JSON format" << endl;
return 1;
}
int nspaces = 4;
int space = ' ';
PrettyVisitor prettyVisitor(nspaces, space);
document.Accept(prettyVisitor);
cout << prettyVisitor.GetResult() << "\n";
return 0;
}