Skip to content

Commit beb4fce

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

7 files changed

Lines changed: 450 additions & 543 deletions

File tree

src/main/java/org/json/JSONArray.java

Lines changed: 6 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,11 @@ public String join(String separator) throws JSONException {
626626
}
627627

628628
StringBuilder sb = new StringBuilder(
629-
JSONObject.valueToString(this.myArrayList.get(0)));
629+
JSONWriter.valueToString(this.myArrayList.get(0)));
630630

631631
for (int i = 1; i < len; i++) {
632632
sb.append(separator)
633-
.append(JSONObject.valueToString(this.myArrayList.get(i)));
633+
.append(JSONWriter.valueToString(this.myArrayList.get(i)));
634634
}
635635
return sb.toString();
636636
}
@@ -1705,7 +1705,7 @@ public JSONObject toJSONObject(JSONArray names) throws JSONException {
17051705
@Override
17061706
public String toString() {
17071707
try {
1708-
return this.toString(0);
1708+
return JSONWriter.format(this, 0);
17091709
} catch (Exception e) {
17101710
return null;
17111711
}
@@ -1740,11 +1740,7 @@ public String toString() {
17401740
*/
17411741
@SuppressWarnings("resource")
17421742
public String toString(int indentFactor) throws JSONException {
1743-
// each value requires a comma, so multiply the count by 2
1744-
// We don't want to oversize the initial capacity
1745-
int initialSize = myArrayList.size() * commaMultiplier;
1746-
Writer sw = new StringBuilderWriter(Math.max(initialSize, minimumBufferSize));
1747-
return this.write(sw, indentFactor, 0).toString();
1743+
return JSONWriter.format(this, indentFactor);
17481744
}
17491745

17501746
/**
@@ -1758,7 +1754,7 @@ public String toString(int indentFactor) throws JSONException {
17581754
* @throws JSONException if a called function fails
17591755
*/
17601756
public Writer write(Writer writer) throws JSONException {
1761-
return this.write(writer, 0, 0);
1757+
return JSONWriter.format(this, writer, 0, 0);
17621758
}
17631759

17641760
/**
@@ -1779,116 +1775,7 @@ public Writer write(Writer writer) throws JSONException {
17791775
*/
17801776
@SuppressWarnings("resource")
17811777
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
1782-
try {
1783-
writeArrayStart(writer);
1784-
writeArrayContents(writer, indentFactor, indent);
1785-
writeArrayEnd(writer);
1786-
return writer;
1787-
} catch (IOException e) {
1788-
throw new JSONException(e);
1789-
}
1790-
}
1791-
1792-
/**
1793-
* Writes the opening bracket '[' of the JSONArray to the writer.
1794-
*
1795-
* @param writer The writer to which the opening bracket is written.
1796-
* @throws IOException If an I/O error occurs while writing.
1797-
*/
1798-
private void writeArrayStart(Writer writer) throws IOException {
1799-
writer.write('[');
1800-
}
1801-
1802-
/**
1803-
* Writes the contents of the JSONArray to the writer, handling single and multiple elements.
1804-
*
1805-
* @param writer The writer to which the contents are written.
1806-
* @param indentFactor The number of spaces to add to each level of indentation.
1807-
* @param indent The indentation level of the top level.
1808-
* @throws JSONException If an error occurs while writing the contents.
1809-
* @throws IOException If an I/O error occurs while writing.
1810-
*/
1811-
private void writeArrayContents(Writer writer, int indentFactor, int indent) throws JSONException, IOException {
1812-
int length = this.length();
1813-
if (length == 1) {
1814-
writeSingleElement(writer, indentFactor, indent);
1815-
} else if (length != 0) {
1816-
writeMultipleElements(writer, indentFactor, indent);
1817-
}
1818-
}
1819-
1820-
/**
1821-
* Writes a single element of the JSONArray to the writer.
1822-
*
1823-
* @param writer The writer to which the element is written.
1824-
* @param indentFactor The number of spaces to add to each level of indentation.
1825-
* @param indent The indentation level of the top level.
1826-
* @throws JSONException If an error occurs while writing the element.
1827-
*/
1828-
private void writeSingleElement(Writer writer, int indentFactor, int indent) throws JSONException {
1829-
try {
1830-
JSONObject.writeValue(writer, this.myArrayList.get(0), indentFactor, indent);
1831-
} catch (Exception e) {
1832-
throw new JSONException("Unable to write JSONArray value at index: 0", e);
1833-
}
1834-
}
1835-
1836-
/**
1837-
* Writes multiple elements of the JSONArray to the writer, formatting them with proper indentation.
1838-
*
1839-
* @param writer The writer to which the elements are written.
1840-
* @param indentFactor The number of spaces to add to each level of indentation.
1841-
* @param indent The indentation level of the top level.
1842-
* @throws JSONException If an error occurs while writing the elements.
1843-
* @throws IOException If an I/O error occurs while writing.
1844-
*/
1845-
private void writeMultipleElements(Writer writer, int indentFactor, int indent) throws JSONException, IOException {
1846-
final int newIndent = indent + indentFactor;
1847-
boolean needsComma = false;
1848-
1849-
for (int i = 0; i < this.length(); i += 1) {
1850-
if (needsComma) {
1851-
writer.write(',');
1852-
}
1853-
if (indentFactor > 0) {
1854-
writer.write('\n');
1855-
}
1856-
JSONObject.indent(writer, newIndent);
1857-
writeElement(writer, i, indentFactor, newIndent);
1858-
needsComma = true;
1859-
}
1860-
1861-
if (indentFactor > 0) {
1862-
writer.write('\n');
1863-
}
1864-
JSONObject.indent(writer, indent);
1865-
}
1866-
1867-
/**
1868-
* Writes a specific element of the JSONArray to the writer.
1869-
*
1870-
* @param writer The writer to which the element is written.
1871-
* @param index The index of the element to write.
1872-
* @param indentFactor The number of spaces to add to each level of indentation.
1873-
* @param indent The indentation level of the top level.
1874-
* @throws JSONException If an error occurs while writing the element.
1875-
*/
1876-
private void writeElement(Writer writer, int index, int indentFactor, int indent) throws JSONException {
1877-
try {
1878-
JSONObject.writeValue(writer, this.myArrayList.get(index), indentFactor, indent);
1879-
} catch (Exception e) {
1880-
throw new JSONException("Unable to write JSONArray value at index: " + index, e);
1881-
}
1882-
}
1883-
1884-
/**
1885-
* Writes the closing bracket ']' of the JSONArray to the writer.
1886-
*
1887-
* @param writer The writer to which the closing bracket is written.
1888-
* @throws IOException If an I/O error occurs while writing.
1889-
*/
1890-
private void writeArrayEnd(Writer writer) throws IOException {
1891-
writer.write(']');
1778+
return JSONWriter.format(this, writer, indentFactor, indent);
18921779
}
18931780

18941781
/**

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

Lines changed: 5 additions & 144 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 JSONStringer.format(this, 0);
2610+
return JSONWriter.format(this, 0);
26112611
} catch (Exception e) {
26122612
return null;
26132613
}
@@ -2641,39 +2641,7 @@ public String toString() {
26412641
*/
26422642
@SuppressWarnings("resource")
26432643
public String toString(int indentFactor) throws JSONException {
2644-
return JSONStringer.format(this, indentFactor);
2645-
}
2646-
2647-
/**
2648-
* Make a JSON text of an Object value. If the object has an
2649-
* value.toJSONString() method, then that method will be used to produce the
2650-
* JSON text. The method is required to produce a strictly conforming text.
2651-
* If the object does not contain a toJSONString method (which is the most
2652-
* common case), then a text will be produced by other means. If the value
2653-
* is an array or Collection, then a JSONArray will be made from it and its
2654-
* toJSONString method will be called. If the value is a MAP, then a
2655-
* JSONObject will be made from it and its toJSONString method will be
2656-
* called. Otherwise, the value's toString method will be called, and the
2657-
* result will be quoted.
2658-
*
2659-
* <p>
2660-
* Warning: This method assumes that the data structure is acyclical.
2661-
*
2662-
* @param value
2663-
* The value to be serialized.
2664-
* @return a printable, displayable, transmittable representation of the
2665-
* object, beginning with <code>{</code>&nbsp;<small>(left
2666-
* brace)</small> and ending with <code>}</code>&nbsp;<small>(right
2667-
* brace)</small>.
2668-
* @throws JSONException
2669-
* If the value is or contains an invalid number.
2670-
*/
2671-
public static String valueToString(Object value) throws JSONException {
2672-
// moves the implementation to JSONWriter as:
2673-
// 1. It makes more sense to be part of the writer class
2674-
// 2. For Android support this method is not available. By implementing it in the Writer
2675-
// Android users can use the writer with the built in Android JSONObject implementation.
2676-
return JSONWriter.valueToString(value);
2644+
return JSONWriter.format(this, indentFactor);
26772645
}
26782646

26792647
/**
@@ -2774,63 +2742,7 @@ private static Object wrap(Object object, Set<Object> objectsRecord, int recursi
27742742
* @throws JSONException if a called function has an error
27752743
*/
27762744
public Writer write(Writer writer) throws JSONException {
2777-
return this.write(writer, 0, 0);
2778-
}
2779-
2780-
@SuppressWarnings("resource")
2781-
static final Writer writeValue(Writer writer, Object value,
2782-
int indentFactor, int indent) throws JSONException, IOException {
2783-
if (value == null || value.equals(null)) {
2784-
writer.write("null");
2785-
} else if (value instanceof JSONString) {
2786-
// JSONString must be checked first, so it can overwrite behaviour of other types below
2787-
Object o;
2788-
try {
2789-
o = ((JSONString) value).toJSONString();
2790-
} catch (Exception e) {
2791-
throw new JSONException(e);
2792-
}
2793-
writer.write(o != null ? o.toString() : quote(value.toString()));
2794-
} else if (value instanceof String) {
2795-
// assuming most values are Strings, so testing it early
2796-
quote(value.toString(), writer);
2797-
return writer;
2798-
} else if (value instanceof Number) {
2799-
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
2800-
final String numberAsString = numberToString((Number) value);
2801-
if(NUMBER_PATTERN.matcher(numberAsString).matches()) {
2802-
writer.write(numberAsString);
2803-
} else {
2804-
// The Number value is not a valid JSON number.
2805-
// Instead we will quote it as a string
2806-
quote(numberAsString, writer);
2807-
}
2808-
} else if (value instanceof Boolean) {
2809-
writer.write(value.toString());
2810-
} else if (value instanceof Enum<?>) {
2811-
writer.write(quote(((Enum<?>)value).name()));
2812-
} else if (value instanceof JSONObject) {
2813-
((JSONObject) value).write(writer, indentFactor, indent);
2814-
} else if (value instanceof JSONArray) {
2815-
((JSONArray) value).write(writer, indentFactor, indent);
2816-
} else if (value instanceof Map) {
2817-
Map<?, ?> map = (Map<?, ?>) value;
2818-
new JSONObject(map).write(writer, indentFactor, indent);
2819-
} else if (value instanceof Collection) {
2820-
Collection<?> coll = (Collection<?>) value;
2821-
new JSONArray(coll).write(writer, indentFactor, indent);
2822-
} else if (value.getClass().isArray()) {
2823-
new JSONArray(value).write(writer, indentFactor, indent);
2824-
} else {
2825-
quote(value.toString(), writer);
2826-
}
2827-
return writer;
2828-
}
2829-
2830-
static final void indent(Writer writer, int indent) throws IOException {
2831-
for (int i = 0; i < indent; i += 1) {
2832-
writer.write(' ');
2833-
}
2745+
return JSONWriter.format(this, writer, 0, 0);
28342746
}
28352747

28362748
/**
@@ -2861,59 +2773,8 @@ static final void indent(Writer writer, int indent) throws IOException {
28612773
* occurs
28622774
*/
28632775
@SuppressWarnings("resource")
2864-
public Writer write(Writer writer, int indentFactor, int indent)
2865-
throws JSONException {
2866-
try {
2867-
boolean needsComma = false;
2868-
final int length = this.length();
2869-
writer.write('{');
2870-
2871-
if (length == 1) {
2872-
final Entry<String,?> entry = this.entrySet().iterator().next();
2873-
final String key = entry.getKey();
2874-
writer.write(quote(key));
2875-
writer.write(':');
2876-
if (indentFactor > 0) {
2877-
writer.write(' ');
2878-
}
2879-
try{
2880-
writeValue(writer, entry.getValue(), indentFactor, indent);
2881-
} catch (Exception e) {
2882-
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
2883-
}
2884-
} else if (length != 0) {
2885-
final int newIndent = indent + indentFactor;
2886-
for (final Entry<String,?> entry : this.entrySet()) {
2887-
if (needsComma) {
2888-
writer.write(',');
2889-
}
2890-
if (indentFactor > 0) {
2891-
writer.write('\n');
2892-
}
2893-
indent(writer, newIndent);
2894-
final String key = entry.getKey();
2895-
writer.write(quote(key));
2896-
writer.write(':');
2897-
if (indentFactor > 0) {
2898-
writer.write(' ');
2899-
}
2900-
try {
2901-
writeValue(writer, entry.getValue(), indentFactor, newIndent);
2902-
} catch (Exception e) {
2903-
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
2904-
}
2905-
needsComma = true;
2906-
}
2907-
if (indentFactor > 0) {
2908-
writer.write('\n');
2909-
}
2910-
indent(writer, indent);
2911-
}
2912-
writer.write('}');
2913-
return writer;
2914-
} catch (IOException exception) {
2915-
throw new JSONException(exception);
2916-
}
2776+
public Writer write(Writer writer, int indentFactor, int indent) throws JSONException {
2777+
return JSONWriter.format(this, writer, indentFactor, indent);
29172778
}
29182779

29192780
/**

0 commit comments

Comments
 (0)