Skip to content

Commit 3e34a78

Browse files
author
alwinsanil
committed
Insufficient Modularization/Feature Envy fix
1 parent 4af5af9 commit 3e34a78

2 files changed

Lines changed: 110 additions & 6 deletions

File tree

src/main/java/org/json/JSONObject.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,7 +2607,7 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
26072607
@Override
26082608
public String toString() {
26092609
try {
2610-
return this.toString(0);
2610+
return JSONStringer.format(this, 0);
26112611
} catch (Exception e) {
26122612
return null;
26132613
}
@@ -2641,11 +2641,7 @@ public String toString() {
26412641
*/
26422642
@SuppressWarnings("resource")
26432643
public String toString(int indentFactor) throws JSONException {
2644-
// 6 characters are the minimum to serialise a key value pair e.g.: "k":1,
2645-
// and we don't want to oversize the initial capacity
2646-
int initialSize = map.size() * 6;
2647-
Writer w = new StringBuilderWriter(Math.max(initialSize, 16));
2648-
return this.write(w, indentFactor, 0).toString();
2644+
return JSONStringer.format(this, indentFactor);
26492645
}
26502646

26512647
/**

src/main/java/org/json/JSONStringer.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
Public Domain.
55
*/
66

7+
import java.io.IOException;
78
import java.io.StringWriter;
9+
import java.io.Writer;
10+
import java.util.Collection;
11+
import java.util.Map;
812

913
/**
1014
* JSONStringer provides a quick and convenient way of producing JSON text
@@ -150,4 +154,108 @@ public JSONStringer value(Object value) throws JSONException {
150154
public String toString() {
151155
return writer.mode == 'd' ? stringWriter.toString() : null;
152156
}
157+
158+
/**
159+
* Format a JSONObject to string with indentation
160+
*/
161+
public static String format(JSONObject jsonObject, int indentFactor) throws JSONException {
162+
StringBuilderWriter w = new StringBuilderWriter(Math.max(jsonObject.length() * 6, 16));
163+
format(jsonObject, w, indentFactor, 0);
164+
return w.toString();
165+
}
166+
167+
/**
168+
* Format a JSONObject to a Writer with indentation
169+
*/
170+
public static Writer format(JSONObject jsonObject, Writer writer,
171+
int indentFactor, int indent) throws JSONException {
172+
try {
173+
boolean needsComma = false;
174+
final int length = jsonObject.length();
175+
writer.write('{');
176+
177+
if (length == 1) {
178+
Map.Entry<String,?> entry = jsonObject.entrySet().iterator().next();
179+
writeEntry(writer, entry, indentFactor, indent);
180+
} else if (length != 0) {
181+
final int newIndent = indent + indentFactor;
182+
for (Map.Entry<String,?> entry : jsonObject.entrySet()) {
183+
if (needsComma) {
184+
writer.write(',');
185+
}
186+
if (indentFactor > 0) {
187+
writer.write('\n');
188+
}
189+
indent(writer, newIndent);
190+
writeEntry(writer, entry, indentFactor, newIndent);
191+
needsComma = true;
192+
}
193+
if (indentFactor > 0) {
194+
writer.write('\n');
195+
}
196+
indent(writer, indent);
197+
}
198+
writer.write('}');
199+
return writer;
200+
} catch (IOException exception) {
201+
throw new JSONException(exception);
202+
}
203+
}
204+
205+
private static void writeEntry(Writer writer, Map.Entry<String,?> entry,
206+
int indentFactor, int indent) throws IOException, JSONException {
207+
String key = entry.getKey();
208+
writer.write(JSONObject.quote(key));
209+
writer.write(':');
210+
if (indentFactor > 0) {
211+
writer.write(' ');
212+
}
213+
writeValue(writer, entry.getValue(), indentFactor, indent);
214+
}
215+
216+
private static void writeValue(Writer writer, Object value,
217+
int indentFactor, int indent) throws JSONException, IOException {
218+
if (value == null || value.equals(null)) {
219+
writer.write("null");
220+
} else if (value instanceof JSONString) {
221+
Object o;
222+
try {
223+
o = ((JSONString) value).toJSONString();
224+
} catch (Exception e) {
225+
throw new JSONException(e);
226+
}
227+
writer.write(o != null ? o.toString() : JSONObject.quote(value.toString()));
228+
} else if (value instanceof String) {
229+
JSONObject.quote(value.toString(), writer);
230+
} else if (value instanceof Number) {
231+
String numberAsString = JSONObject.numberToString((Number) value);
232+
if(JSONObject.NUMBER_PATTERN.matcher(numberAsString).matches()) {
233+
writer.write(numberAsString);
234+
} else {
235+
JSONObject.quote(numberAsString, writer);
236+
}
237+
} else if (value instanceof Boolean) {
238+
writer.write(value.toString());
239+
} else if (value instanceof Enum<?>) {
240+
writer.write(JSONObject.quote(((Enum<?>)value).name()));
241+
} else if (value instanceof JSONObject) {
242+
format((JSONObject) value, writer, indentFactor, indent);
243+
} else if (value instanceof JSONArray) {
244+
((JSONArray) value).write(writer, indentFactor, indent);
245+
} else if (value instanceof Map) {
246+
new JSONObject((Map<?,?>) value).write(writer, indentFactor, indent);
247+
} else if (value instanceof Collection) {
248+
new JSONArray((Collection<?>) value).write(writer, indentFactor, indent);
249+
} else if (value.getClass().isArray()) {
250+
new JSONArray(value).write(writer, indentFactor, indent);
251+
} else {
252+
JSONObject.quote(value.toString(), writer);
253+
}
254+
}
255+
256+
private static void indent(Writer writer, int indent) throws IOException {
257+
for (int i = 0; i < indent; i += 1) {
258+
writer.write(' ');
259+
}
260+
}
153261
}

0 commit comments

Comments
 (0)