mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-02 11:05:28 -04:00
Merge pull request #867 from Simulant87/863-improve-toString-performance-StringBuilderWriter
Improve toString Performance: Use StringBuilderWriter for toString methods
This commit is contained in:
commit
45dede448c
@ -5,7 +5,6 @@ Public Domain.
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Array;
|
||||
import java.math.BigDecimal;
|
||||
@ -1695,7 +1694,10 @@ public class JSONArray implements Iterable<Object> {
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public String toString(int indentFactor) throws JSONException {
|
||||
StringWriter sw = new StringWriter();
|
||||
// each value requires a comma, so multiply the count by 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();
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,6 @@ Public Domain.
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
@ -2248,7 +2247,10 @@ public class JSONObject {
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public static String quote(String string) {
|
||||
StringWriter sw = new StringWriter();
|
||||
if (string == null || string.isEmpty()) {
|
||||
return "\"\"";
|
||||
}
|
||||
Writer sw = new StringBuilderWriter(string.length() + 2);
|
||||
try {
|
||||
return quote(string, sw).toString();
|
||||
} catch (IOException ignored) {
|
||||
@ -2647,7 +2649,10 @@ public class JSONObject {
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public String toString(int indentFactor) throws JSONException {
|
||||
StringWriter w = new StringWriter();
|
||||
// 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();
|
||||
}
|
||||
|
||||
@ -2790,6 +2795,7 @@ public class JSONObject {
|
||||
if (value == null || value.equals(null)) {
|
||||
writer.write("null");
|
||||
} else if (value instanceof JSONString) {
|
||||
// JSONString must be checked first, so it can overwrite behaviour of other types below
|
||||
Object o;
|
||||
try {
|
||||
o = ((JSONString) value).toJSONString();
|
||||
@ -2797,6 +2803,10 @@ public class JSONObject {
|
||||
throw new JSONException(e);
|
||||
}
|
||||
writer.write(o != null ? o.toString() : quote(value.toString()));
|
||||
} else if (value instanceof String) {
|
||||
// assuming most values are Strings, so testing it early
|
||||
quote(value.toString(), writer);
|
||||
return writer;
|
||||
} else if (value instanceof Number) {
|
||||
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
|
||||
final String numberAsString = numberToString((Number) value);
|
||||
|
92
src/main/java/org/json/StringBuilderWriter.java
Normal file
92
src/main/java/org/json/StringBuilderWriter.java
Normal file
@ -0,0 +1,92 @@
|
||||
package org.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* Performance optimised alternative for {@link java.io.StringWriter}
|
||||
* using internally a {@link StringBuilder} instead of a {@link StringBuffer}.
|
||||
*/
|
||||
public class StringBuilderWriter extends Writer {
|
||||
private final StringBuilder builder;
|
||||
|
||||
/**
|
||||
* Create a new string builder writer using the default initial string-builder buffer size.
|
||||
*/
|
||||
public StringBuilderWriter() {
|
||||
builder = new StringBuilder();
|
||||
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
|
||||
*/
|
||||
public StringBuilderWriter(int initialSize) {
|
||||
builder = new StringBuilder(initialSize);
|
||||
lock = builder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int c) {
|
||||
builder.append((char) c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(char[] cbuf, int offset, int length) {
|
||||
if ((offset < 0) || (offset > cbuf.length) || (length < 0) ||
|
||||
((offset + length) > cbuf.length) || ((offset + length) < 0)) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else if (length == 0) {
|
||||
return;
|
||||
}
|
||||
builder.append(cbuf, offset, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String str) {
|
||||
builder.append(str);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(String str, int offset, int length) {
|
||||
builder.append(str, offset, offset + length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringBuilderWriter append(CharSequence csq) {
|
||||
write(String.valueOf(csq));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringBuilderWriter append(CharSequence csq, int start, int end) {
|
||||
if (csq == null) {
|
||||
csq = "null";
|
||||
}
|
||||
return append(csq.subSequence(start, end));
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringBuilderWriter append(char c) {
|
||||
write(c);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
}
|
@ -319,6 +319,22 @@ public class JSONStringTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnumJSONString() {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("key", MyEnum.MY_ENUM);
|
||||
assertEquals("{\"key\":\"myJsonString\"}", jsonObject.toString());
|
||||
}
|
||||
|
||||
private enum MyEnum implements JSONString {
|
||||
MY_ENUM;
|
||||
|
||||
@Override
|
||||
public String toJSONString() {
|
||||
return "\"myJsonString\"";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A JSONString that returns a valid JSON string value.
|
||||
*/
|
||||
|
60
src/test/java/org/json/junit/StringBuilderWriterTest.java
Normal file
60
src/test/java/org/json/junit/StringBuilderWriterTest.java
Normal file
@ -0,0 +1,60 @@
|
||||
package org.json.junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.json.StringBuilderWriter;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringBuilderWriterTest {
|
||||
private StringBuilderWriter writer;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
writer = new StringBuilderWriter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteChar() {
|
||||
writer.write('a');
|
||||
assertEquals("a", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteCharArray() {
|
||||
char[] chars = {'a', 'b', 'c'};
|
||||
writer.write(chars, 0, 3);
|
||||
assertEquals("abc", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteString() {
|
||||
writer.write("hello");
|
||||
assertEquals("hello", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWriteStringWithOffsetAndLength() {
|
||||
writer.write("hello world", 6, 5);
|
||||
assertEquals("world", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendCharSequence() {
|
||||
writer.append("hello");
|
||||
assertEquals("hello", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendCharSequenceWithStartAndEnd() {
|
||||
CharSequence csq = "hello world";
|
||||
writer.append(csq, 6, 11);
|
||||
assertEquals("world", writer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppendChar() {
|
||||
writer.append('a');
|
||||
assertEquals("a", writer.toString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user