Skip to content

Commit

Permalink
#863 compute initial capacity for StringBuilderWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Simulant87 committed Feb 24, 2024
1 parent 6660e40 commit 06778bd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,10 @@ public String toString() {
*/
@SuppressWarnings("resource")
public String toString(int indentFactor) throws JSONException {
Writer sw = new StringBuilderWriter();
// each value requires a comma, so multiply the count my 2
// We don't want to oversize the initial capacity
int initialSize = myArrayList.size() * 2;
Writer sw = new StringBuilderWriter(Math.max(initialSize, 16));
return this.write(sw, indentFactor, 0).toString();
}

Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,10 @@ public Object optQuery(JSONPointer jsonPointer) {
*/
@SuppressWarnings("resource")
public static String quote(String string) {
Writer sw = new StringBuilderWriter();
if (string == null || string.isEmpty()) {
return "\"\"";
}
Writer sw = new StringBuilderWriter(string.length() + 2);
try {
return quote(string, sw).toString();
} catch (IOException ignored) {
Expand Down Expand Up @@ -2557,7 +2560,10 @@ public String toString() {
*/
@SuppressWarnings("resource")
public String toString(int indentFactor) throws JSONException {
Writer w = new StringBuilderWriter();
// 6 characters are the minimum to serialise a key value pair e.g.: "k":1,
// and we don't want to oversize the initial capacity
int initialSize = map.size() * 6;
Writer w = new StringBuilderWriter(Math.max(initialSize, 16));
return this.write(w, indentFactor, 0).toString();
}

Expand Down Expand Up @@ -2699,14 +2705,18 @@ static final Writer writeValue(Writer writer, Object value,
int indentFactor, int indent) throws JSONException, IOException {
if (value == null || value.equals(null)) {
writer.write("null");
} else if (value instanceof String) {
// assuming most values are Strings, so testing it earlier
quote(value.toString(), writer);
return writer;
} else if (value instanceof JSONString) {
Object o;
try {
o = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
writer.write(o != null ? o.toString() : quote(value.toString()));
writer.write(o != null ? o.toString() : "\"\"");
} else if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
final String numberAsString = numberToString((Number) value);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/json/StringBuilderWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ class StringBuilderWriter extends Writer {
lock = builder;
}

/**
* Create a new string builder writer using the specified initial string-builder buffer size.
*
* @param initialSize The number of {@code char} values that will fit into this buffer
* before it is automatically expanded
*
* @throws IllegalArgumentException If {@code initialSize} is negative
*/
StringBuilderWriter(int initialSize) {
builder = new StringBuilder(initialSize);
lock = builder;
}

@Override
public void write(int c) {
builder.append((char) c);
Expand Down

0 comments on commit 06778bd

Please sign in to comment.