JsonReader
This commit is contained in:
@@ -37,9 +37,9 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
|
||||
private final ThreadLocal<JsonReader> readerPool = Utility.withInitialThreadLocal(JsonReader::new);
|
||||
|
||||
private Encodeable lastConvertEncodeable;
|
||||
private Encodeable lastEncodeable;
|
||||
|
||||
private Decodeable lastConvertDecodeable;
|
||||
private Decodeable lastDecodeable;
|
||||
|
||||
protected JsonConvert(JsonFactory factory, int features) {
|
||||
super(factory, features);
|
||||
@@ -192,12 +192,14 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
if (text == null || type == null) {
|
||||
return null;
|
||||
}
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
Decodeable decoder = this.lastDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
T rs = (T) decoder.convertFrom(new JsonReader(text, offset, length));
|
||||
JsonReader reader = pollReader().setText(text, offset, length);
|
||||
T rs = (T) decoder.convertFrom(reader);
|
||||
offerReader(reader);
|
||||
return rs;
|
||||
}
|
||||
|
||||
@@ -205,10 +207,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
if (type == null || in == null) {
|
||||
return null;
|
||||
}
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
Decodeable decoder = this.lastDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
return (T) decoder.convertFrom(new JsonStreamReader(in));
|
||||
}
|
||||
@@ -218,10 +220,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
if (type == null || buffers == null || buffers.length == 0) {
|
||||
return null;
|
||||
}
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
Decodeable decoder = this.lastDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
return (T) decoder.convertFrom(new JsonByteBufferReader(buffers));
|
||||
}
|
||||
@@ -231,10 +233,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
Decodeable decoder = this.lastConvertDecodeable;
|
||||
Decodeable decoder = this.lastDecodeable;
|
||||
if (decoder == null || decoder.getType() != type) {
|
||||
decoder = factory.loadDecoder(type);
|
||||
this.lastConvertDecodeable = decoder;
|
||||
this.lastDecodeable = decoder;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
T rs = (T) decoder.convertFrom(reader);
|
||||
@@ -325,10 +327,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
}
|
||||
JsonCharsWriter writer = pollJsonCharsWriter();
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -347,10 +349,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
}
|
||||
JsonBytesWriter writer = pollJsonBytesWriter();
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -369,10 +371,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -390,10 +392,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -409,10 +411,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
} else {
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
JsonStreamWriter writer = configWrite(new JsonStreamWriter(features, out));
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
@@ -441,10 +443,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
final Type t = type == null ? value.getClass() : type;
|
||||
Encodeable encoder = this.lastConvertEncodeable;
|
||||
Encodeable encoder = this.lastEncodeable;
|
||||
if (encoder == null || encoder.getType() != t) {
|
||||
encoder = factory.loadEncoder(t);
|
||||
this.lastConvertEncodeable = encoder;
|
||||
this.lastEncodeable = encoder;
|
||||
}
|
||||
if (encoder.specifyable()) {
|
||||
writer.specificObjectType(t);
|
||||
|
||||
@@ -29,9 +29,6 @@ public class JsonReader extends Reader {
|
||||
|
||||
private CharArray array;
|
||||
|
||||
// public static ObjectPool<JsonReader> createPool(int max) {
|
||||
// return new ObjectPool<>(max, (Object... params) -> new JsonReader(), null, JsonReader::recycle);
|
||||
// }
|
||||
public JsonReader() {}
|
||||
|
||||
public JsonReader(String json) {
|
||||
@@ -46,18 +43,19 @@ public class JsonReader extends Reader {
|
||||
setText(text, start, len);
|
||||
}
|
||||
|
||||
public final void setText(String text) {
|
||||
setText(Utility.charArray(text));
|
||||
public final JsonReader setText(String text) {
|
||||
return setText(Utility.charArray(text));
|
||||
}
|
||||
|
||||
public final void setText(char[] text) {
|
||||
setText(text, 0, text.length);
|
||||
public final JsonReader setText(char[] text) {
|
||||
return setText(text, 0, text.length);
|
||||
}
|
||||
|
||||
public final void setText(char[] text, int start, int len) {
|
||||
public final JsonReader setText(char[] text, int start, int len) {
|
||||
this.text = text;
|
||||
this.position = start - 1;
|
||||
this.limit = start + len - 1;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -72,7 +70,7 @@ public class JsonReader extends Reader {
|
||||
this.limit = -1;
|
||||
this.text = null;
|
||||
if (this.array != null) {
|
||||
if (this.array.content.length > 102400) {
|
||||
if (this.array.content.length > CharArray.DEFAULT_SIZE * 200) {
|
||||
this.array = null;
|
||||
} else {
|
||||
this.array.clear();
|
||||
@@ -278,7 +276,7 @@ public class JsonReader extends Reader {
|
||||
return null;
|
||||
}
|
||||
throw new ConvertException("a json object text must begin with '{' (position = " + position + ") but '" + ch
|
||||
+ "' in (" + new String(this.text) + ")");
|
||||
+ "' in " + new String(this.text));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -328,7 +326,7 @@ public class JsonReader extends Reader {
|
||||
return SIGN_NULL;
|
||||
}
|
||||
throw new ConvertException("a json array text must begin with '[' (position = " + position + ") but '" + ch
|
||||
+ "' in (" + new String(this.text) + ")");
|
||||
+ "' in " + new String(this.text));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1050,9 +1048,11 @@ public class JsonReader extends Reader {
|
||||
|
||||
protected static class CharArray {
|
||||
|
||||
private static final int DEFAULT_SIZE = 1024;
|
||||
|
||||
private int count;
|
||||
|
||||
private char[] content = new char[1024];
|
||||
private char[] content = new char[DEFAULT_SIZE];
|
||||
|
||||
private char[] expand(int len) {
|
||||
int newcount = count + len;
|
||||
|
||||
Reference in New Issue
Block a user