TidyJSONWriter.java
5.44 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.json.io.JSONWriter
*/
package com.day.cq.commons;
import java.io.IOException;
import java.io.Writer;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;
public class TidyJSONWriter
extends JSONWriter {
private static final int maxdepth = 50;
private static final String[] INDENTS = new String[50];
private boolean tidy;
private boolean comma = false;
protected char mode = 105;
private char[] stack = new char[50];
private int top = 0;
protected Writer writer;
public TidyJSONWriter(Writer w) {
super(w);
this.writer = w;
}
public boolean isTidy() {
return this.tidy;
}
public void setTidy(boolean tidy) {
this.tidy = tidy;
}
private TidyJSONWriter append(String s) throws JSONException {
if (s == null) {
throw new JSONException("Null pointer");
}
if (this.mode == 'o' || this.mode == 'a') {
try {
if (this.comma && this.mode == 'a') {
this.writer.write(44);
}
if (this.tidy && this.mode == 'a' && !"{".equals(s) && !"[".equals(s)) {
this.writer.write(10);
this.writer.write(INDENTS[this.top]);
}
this.writer.write(s);
}
catch (IOException e) {
throw new JSONException((Throwable)e);
}
if (this.mode == 'o') {
this.mode = 107;
}
this.comma = true;
return this;
}
throw new JSONException("Value out of sequence.");
}
public TidyJSONWriter array() throws JSONException {
if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
this.push('a');
this.append("[");
this.comma = false;
return this;
}
throw new JSONException("Misplaced array.");
}
private TidyJSONWriter end(char m, char c) throws JSONException {
if (this.mode != m) {
throw new JSONException(m == 'o' ? "Misplaced endObject." : "Misplaced endArray.");
}
this.pop(m);
try {
if (this.tidy) {
this.writer.write(10);
this.writer.write(INDENTS[this.top]);
}
this.writer.write(c);
}
catch (IOException e) {
throw new JSONException((Throwable)e);
}
this.comma = true;
return this;
}
public TidyJSONWriter endArray() throws JSONException {
return this.end('a', ']');
}
public TidyJSONWriter endObject() throws JSONException {
return this.end('k', '}');
}
public TidyJSONWriter key(String s) throws JSONException {
if (s == null) {
throw new JSONException("Null key.");
}
if (this.mode == 'k') {
try {
if (this.comma) {
this.writer.write(44);
}
if (this.tidy) {
this.writer.write(10);
this.writer.write(INDENTS[this.top]);
}
this.writer.write(JSONObject.quote((String)s));
this.writer.write(58);
if (this.tidy) {
this.writer.write(32);
}
this.comma = false;
this.mode = 111;
return this;
}
catch (IOException e) {
throw new JSONException((Throwable)e);
}
}
throw new JSONException("Misplaced key.");
}
public TidyJSONWriter object() throws JSONException {
if (this.mode == 'i') {
this.mode = 111;
}
if (this.mode == 'o' || this.mode == 'a') {
this.append("{");
this.push('k');
this.comma = false;
return this;
}
throw new JSONException("Misplaced object.");
}
private void pop(char c) throws JSONException {
if (this.top <= 0 || this.stack[this.top - 1] != c) {
throw new JSONException("Nesting error.");
}
--this.top;
this.mode = this.top == 0 ? 100 : this.stack[this.top - 1];
}
private void push(char c) throws JSONException {
if (this.top >= 50) {
throw new JSONException("Nesting too deep (maximum is 50 levels)");
}
this.stack[this.top] = c;
this.mode = c;
++this.top;
}
public TidyJSONWriter value(boolean b) throws JSONException {
return this.append(b ? "true" : "false");
}
public TidyJSONWriter value(double d) throws JSONException {
return this.value(new Double(d));
}
public TidyJSONWriter value(long l) throws JSONException {
return this.append(Long.toString(l));
}
public TidyJSONWriter value(Object o) throws JSONException {
return this.append(JSONObject.valueToString((Object)o));
}
static {
StringBuffer indent = new StringBuffer();
for (int i = 0; i < INDENTS.length; ++i) {
TidyJSONWriter.INDENTS[i] = indent.toString();
indent.append(" ");
}
}
}