-
Notifications
You must be signed in to change notification settings - Fork 180
/
JSONFormatter.pas
230 lines (218 loc) · 4.93 KB
/
JSONFormatter.pas
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
unit JSONFormatter;
interface
uses
System.SysUtils, System.Classes, System.Math;
type
TJSONFormatter = class
private
class function RemoveWhiteSpace(const aInput : string) : string;
class function getSpaces(aQuantity : integer) : string;
class function InsertLineBreaks(const aInput: string): string;
class function RemoveEmptyLines(const aInput: string): string;
class function Indent(const aInput: string): string;
public
class function FormatJSON(const aInput: string): string;
end;
implementation
{ TJSONFormatter }
class function TJSONFormatter.FormatJSON(const aInput: string): string;
begin
// Clean the input from previous formatting
result := RemoveWhiteSpace(aInput);
// Split up logical units of JSON
result := InsertLineBreaks(result);
// It's easier to clean up empty lines then preventing them
result := RemoveEmptyLines(result);
// Indent each line with the correct space
result := Indent(result);
end;
class function TJSONFormatter.getSpaces(aQuantity: integer): string;
begin
result := '';
while aQuantity > 0 do
begin
result := result + ' ';
dec(aQuantity);
end;
end;
class function TJSONFormatter.RemoveWhiteSpace(const aInput: string): string;
const
whitespace = [#0, #8, #9, #10, #12, #13, ' '];
var
i: integer;
insideString: boolean;
begin
i := 1;
insideString := false;
while i <= length(aInput) do
begin
if aInput[i] = '\' then
begin
result := result + aInput[i] + aInput[i + 1];
inc(i, 2);
end
else if aInput[i] = '"' then
begin
result := result + aInput[i];
insideString := not insideString;
inc(i);
end
else if not insideString and CharInSet(aInput[i], whitespace) then
inc(i)
else
begin
result := result + aInput[i];
inc(i);
end;
end;
end;
class function TJSONFormatter.Indent(const aInput: string): string;
var
sl: TStringList;
i: integer;
lvl: integer;
begin
lvl := 0;
sl := TStringList.Create;
try
sl.Text := aInput;
for i := 0 to sl.Count - 1 do
begin
case sl[i][1] of
'{':
begin
sl[i] := getSpaces(lvl * 2) + sl[i];
if sl[i][2] <> '}' then
inc(lvl);
end;
'[':
begin
sl[i] := getSpaces(lvl * 2) + sl[i];
if sl[i][2] <> ']' then
inc(lvl);
end;
'}', ']':
begin
dec(lvl);
lvl := max(lvl, 0);
sl[i] := getSpaces(lvl * 2) + sl[i];
end
else
sl[i] := getSpaces(lvl * 2) + sl[i];
end;
end;
result := sl.Text;
finally
sl.Free;
end;
end;
class function TJSONFormatter.InsertLineBreaks(const aInput: string): string;
var
i: integer;
insideString: boolean;
s: string;
begin
s := '';
i := 1;
insideString := false;
while i <= length(aInput) do
begin
if insideString then
begin
s := s + aInput[i];
if aInput[i] = '\' then
begin
s := s + aInput[i + 1];
inc(i, 2);
end;
if aInput[i] = '"' then
insideString := false;
inc(i);
end
else
begin
case aInput[i] of
'\':
begin
s := s + aInput[i] + aInput[i + 1];
inc(i, 2);
end;
'"':
begin
s := s + aInput[i];
insideString := not insideString;
inc(i);
end;
'{':
begin
if aInput[i + 1] = '}' then
begin
s := s + '{}';
inc(i, 2);
end
else
begin
s := s + sLineBreak + aInput[i] + sLineBreak;
inc(i);
end;
end;
'[':
begin
if aInput[i + 1] = ']' then
begin
s := s + '[]';
inc(i, 2);
end
else
begin
s := s + sLineBreak + aInput[i] + sLineBreak;
inc(i);
end;
end;
'}', ']':
begin
if (length(aInput) > i) and (aInput[i + 1] = ',') then
begin
s := s + sLineBreak + aInput[i] + ',' + sLineBreak;
inc(i, 2);
end
else
begin
s := s + sLineBreak + aInput[i] + sLineBreak;
inc(i);
end;
end;
',':
begin
s := s + aInput[i] + sLineBreak;
inc(i);
end;
else
begin
s := s + aInput[i];
inc(i);
end;
end;
end;
end;
result := s;
end;
class function TJSONFormatter.RemoveEmptyLines(const aInput: string): string;
var
sl: TStringList;
i: integer;
begin
sl := TStringList.Create;
try
sl.Text := aInput;
for i := sl.Count - 1 downto 0 do
begin
if sl[i] = '' then
sl.Delete(i);
end;
result := sl.Text;
finally
sl.Free;
end;
end;
end.