This commit is contained in:
@@ -30,9 +30,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
public static final Type TYPE_RETRESULT_STRING = new TypeToken<RetResult<String>>() {
|
public static final Type TYPE_RETRESULT_STRING = new TypeToken<RetResult<String>>() {
|
||||||
}.getType();
|
}.getType();
|
||||||
|
|
||||||
private static final ObjectPool<JsonReader> readerPool = JsonReader.createPool(Integer.getInteger("convert.json.pool.size", Integer.getInteger("convert.pool.size", 16)));
|
//private static final ObjectPool<JsonReader> readerPool = JsonReader.createPool(Integer.getInteger("convert.json.pool.size", Integer.getInteger("convert.pool.size", 16)));
|
||||||
|
//private static final ObjectPool<JsonWriter> writerPool = JsonWriter.createPool(Integer.getInteger("convert.json.pool.size", Integer.getInteger("convert.pool.size", 16)));
|
||||||
private static final ObjectPool<JsonWriter> writerPool = JsonWriter.createPool(Integer.getInteger("convert.json.pool.size", Integer.getInteger("convert.pool.size", 16)));
|
//
|
||||||
|
private static final ThreadLocal<JsonWriter> writerPool = ThreadLocal.withInitial(JsonWriter::new);
|
||||||
|
|
||||||
private final boolean tiny;
|
private final boolean tiny;
|
||||||
|
|
||||||
@@ -66,43 +67,41 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------ reader -----------------------------------------------------------
|
//------------------------------ reader -----------------------------------------------------------
|
||||||
public JsonReader pollJsonReader(final ByteBuffer... buffers) {
|
// public JsonReader pollJsonReader(final ByteBuffer... buffers) {
|
||||||
return new JsonByteBufferReader((ConvertMask) null, buffers);
|
// return new JsonByteBufferReader((ConvertMask) null, buffers);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public JsonReader pollJsonReader(final InputStream in) {
|
// public JsonReader pollJsonReader(final InputStream in) {
|
||||||
return new JsonStreamReader(in);
|
// return new JsonStreamReader(in);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public JsonReader pollJsonReader() {
|
// public JsonReader pollJsonReader() {
|
||||||
return readerPool.get();
|
// return readerPool.get();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public void offerJsonReader(final JsonReader in) {
|
// public void offerJsonReader(final JsonReader in) {
|
||||||
if (in != null) readerPool.accept(in);
|
// if (in != null) readerPool.accept(in);
|
||||||
}
|
// }
|
||||||
|
|
||||||
//------------------------------ writer -----------------------------------------------------------
|
//------------------------------ writer -----------------------------------------------------------
|
||||||
public JsonByteBufferWriter pollJsonWriter(final Supplier<ByteBuffer> supplier) {
|
private JsonByteBufferWriter pollJsonWriter(final Supplier<ByteBuffer> supplier) {
|
||||||
return configWrite(new JsonByteBufferWriter(tiny, supplier));
|
return configWrite(new JsonByteBufferWriter(tiny, supplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonWriter pollJsonWriter(final OutputStream out) {
|
private JsonWriter pollJsonWriter(final OutputStream out) {
|
||||||
return configWrite(new JsonStreamWriter(tiny, out));
|
return configWrite(new JsonStreamWriter(tiny, out));
|
||||||
}
|
}
|
||||||
|
//
|
||||||
public JsonWriter pollJsonWriter(final Charset charset, final OutputStream out) {
|
// public JsonWriter pollJsonWriter(final Charset charset, final OutputStream out) {
|
||||||
return configWrite(new JsonStreamWriter(tiny, charset, out));
|
// return configWrite(new JsonStreamWriter(tiny, charset, out));
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public JsonWriter pollJsonWriter() {
|
// public JsonWriter pollJsonWriter() {
|
||||||
return configWrite(writerPool.get().tiny(tiny));
|
// return configWrite(writerPool.get().tiny(tiny));
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public void offerJsonWriter(final JsonWriter writer) {
|
// public void offerJsonWriter(final JsonWriter writer) {
|
||||||
if (writer != null) writerPool.accept(writer);
|
// if (writer != null) writerPool.accept(writer);
|
||||||
}
|
// }
|
||||||
|
|
||||||
//------------------------------ convertFrom -----------------------------------------------------------
|
//------------------------------ convertFrom -----------------------------------------------------------
|
||||||
@Override
|
@Override
|
||||||
public <T> T convertFrom(final Type type, final byte[] bytes) {
|
public <T> T convertFrom(final Type type, final byte[] bytes) {
|
||||||
@@ -128,10 +127,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
|
|
||||||
public <T> T convertFrom(final Type type, final char[] text, final int offset, final int length) {
|
public <T> T convertFrom(final Type type, final char[] text, final int offset, final int length) {
|
||||||
if (text == null || type == null) return null;
|
if (text == null || type == null) return null;
|
||||||
final JsonReader in = readerPool.get();
|
//final JsonReader in = readerPool.get();
|
||||||
in.setText(text, offset, length);
|
//in.setText(text, offset, length);
|
||||||
T rs = (T) factory.loadDecoder(type).convertFrom(in);
|
T rs = (T) factory.loadDecoder(type).convertFrom(new JsonReader(text, offset, length));
|
||||||
readerPool.accept(in);
|
//readerPool.accept(in);
|
||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,10 +173,10 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
//返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
//返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
||||||
public <V> V convertFrom(final char[] text, final int offset, final int length) {
|
public <V> V convertFrom(final char[] text, final int offset, final int length) {
|
||||||
if (text == null) return null;
|
if (text == null) return null;
|
||||||
final JsonReader in = readerPool.get();
|
//final JsonReader in = readerPool.get();
|
||||||
in.setText(text, offset, length);
|
//in.setText(text, offset, length);
|
||||||
Object rs = new AnyDecoder(factory).convertFrom(in);
|
Object rs = new AnyDecoder(factory).convertFrom(new JsonReader(text, offset, length));
|
||||||
readerPool.accept(in);
|
//readerPool.accept(in);
|
||||||
return (V) rs;
|
return (V) rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,11 +215,20 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
public String convertTo(final Type type, final Object value) {
|
public String convertTo(final Type type, final Object value) {
|
||||||
if (type == null) return null;
|
if (type == null) return null;
|
||||||
if (value == null) return "null";
|
if (value == null) return "null";
|
||||||
final JsonWriter writer = pollJsonWriter();
|
JsonWriter writer = writerPool.get();// pollJsonWriter();
|
||||||
|
if (writer == null) {
|
||||||
|
writer = new JsonWriter();
|
||||||
|
} else {
|
||||||
|
writerPool.set(null);
|
||||||
|
}
|
||||||
writer.specify(type);
|
writer.specify(type);
|
||||||
factory.loadEncoder(type).convertTo(writer, value);
|
factory.loadEncoder(type).convertTo(writer, value);
|
||||||
String result = writer.toString();
|
String result = writer.toString();
|
||||||
writerPool.accept(writer);
|
//writerPool.accept(writer);
|
||||||
|
if (writerPool.get() == null) {
|
||||||
|
writer.recycle();
|
||||||
|
writerPool.set(writer);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,21 +243,39 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
public byte[] convertToBytes(final Type type, final Object value) {
|
public byte[] convertToBytes(final Type type, final Object value) {
|
||||||
if (type == null) return null;
|
if (type == null) return null;
|
||||||
if (value == null) return null;
|
if (value == null) return null;
|
||||||
final JsonWriter writer = pollJsonWriter();
|
JsonWriter writer = writerPool.get();// pollJsonWriter();
|
||||||
|
if (writer == null) {
|
||||||
|
writer = new JsonWriter();
|
||||||
|
} else {
|
||||||
|
writerPool.set(null);
|
||||||
|
}
|
||||||
writer.specify(type);
|
writer.specify(type);
|
||||||
factory.loadEncoder(type).convertTo(writer, value);
|
factory.loadEncoder(type).convertTo(writer, value);
|
||||||
String result = writer.toString();
|
String result = writer.toString();
|
||||||
writerPool.accept(writer);
|
//writerPool.accept(writer);
|
||||||
|
if (writerPool.get() == null) {
|
||||||
|
writer.recycle();
|
||||||
|
writerPool.set(writer);
|
||||||
|
}
|
||||||
return result == null ? null : result.getBytes(StandardCharsets.UTF_8);
|
return result == null ? null : result.getBytes(StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String convertMapTo(final Object... values) {
|
public String convertMapTo(final Object... values) {
|
||||||
if (values == null) return "null";
|
if (values == null) return "null";
|
||||||
final JsonWriter writer = pollJsonWriter();
|
JsonWriter writer = writerPool.get();// pollJsonWriter();
|
||||||
|
if (writer == null) {
|
||||||
|
writer = new JsonWriter();
|
||||||
|
} else {
|
||||||
|
writerPool.set(null);
|
||||||
|
}
|
||||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||||
String result = writer.toString();
|
String result = writer.toString();
|
||||||
writerPool.accept(writer);
|
//writerPool.accept(writer);
|
||||||
|
if (writerPool.get() == null) {
|
||||||
|
writer.recycle();
|
||||||
|
writerPool.set(writer);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,11 +292,20 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
if (value == null) {
|
if (value == null) {
|
||||||
pollJsonWriter(out).writeNull();
|
pollJsonWriter(out).writeNull();
|
||||||
} else {
|
} else {
|
||||||
final JsonWriter writer = pollJsonWriter();
|
JsonWriter writer = writerPool.get();// pollJsonWriter();
|
||||||
|
if (writer == null) {
|
||||||
|
writer = new JsonWriter();
|
||||||
|
} else {
|
||||||
|
writerPool.set(null);
|
||||||
|
}
|
||||||
writer.specify(type);
|
writer.specify(type);
|
||||||
factory.loadEncoder(type).convertTo(writer, value);
|
factory.loadEncoder(type).convertTo(writer, value);
|
||||||
byte[] bs = writer.toBytes();
|
byte[] bs = writer.toBytes();
|
||||||
writerPool.accept(writer);
|
//writerPool.accept(writer);
|
||||||
|
if (writerPool.get() == null) {
|
||||||
|
writer.recycle();
|
||||||
|
writerPool.set(writer);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
out.write(bs);
|
out.write(bs);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -283,10 +318,19 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
if (values == null) {
|
if (values == null) {
|
||||||
pollJsonWriter(out).writeNull();
|
pollJsonWriter(out).writeNull();
|
||||||
} else {
|
} else {
|
||||||
final JsonWriter writer = pollJsonWriter();
|
JsonWriter writer = writerPool.get();// pollJsonWriter();
|
||||||
|
if (writer == null) {
|
||||||
|
writer = new JsonWriter();
|
||||||
|
} else {
|
||||||
|
writerPool.set(null);
|
||||||
|
}
|
||||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||||
byte[] bs = writer.toBytes();
|
byte[] bs = writer.toBytes();
|
||||||
writerPool.accept(writer);
|
//writerPool.accept(writer);
|
||||||
|
if (writerPool.get() == null) {
|
||||||
|
writer.recycle();
|
||||||
|
writerPool.set(writer);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
out.write(bs);
|
out.write(bs);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -358,22 +402,32 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonWriter convertToWriter(final Object value) {
|
// public JsonWriter convertToWriter(final Object value) {
|
||||||
if (value == null) return null;
|
// if (value == null) return null;
|
||||||
return convertToWriter(value.getClass(), value);
|
// return convertToWriter(value.getClass(), value);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public JsonWriter convertToWriter(final Type type, final Object value) {
|
// public JsonWriter convertToWriter(final Type type, final Object value) {
|
||||||
if (type == null) return null;
|
// if (type == null) return null;
|
||||||
final JsonWriter writer = pollJsonWriter();
|
// JsonWriter writer = writerPool.get();// pollJsonWriter();
|
||||||
writer.specify(type);
|
// if (writer == null) {
|
||||||
factory.loadEncoder(type).convertTo(writer, value);
|
// writer = new JsonWriter();
|
||||||
return writer;
|
// } else {
|
||||||
}
|
// writerPool.set(null);
|
||||||
|
// }
|
||||||
public JsonWriter convertMapToWriter(final Object... values) {
|
// writer.specify(type);
|
||||||
final JsonWriter writer = pollJsonWriter();
|
// factory.loadEncoder(type).convertTo(writer, value);
|
||||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
// return writer;
|
||||||
return writer;
|
// }
|
||||||
}
|
//
|
||||||
|
// public JsonWriter convertMapToWriter(final Object... values) {
|
||||||
|
// JsonWriter writer = writerPool.get();// pollJsonWriter();
|
||||||
|
// if (writer == null) {
|
||||||
|
// writer = new JsonWriter();
|
||||||
|
// } else {
|
||||||
|
// writerPool.set(null);
|
||||||
|
// }
|
||||||
|
// ((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||||
|
// return writer;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ public class JsonReader extends Reader {
|
|||||||
|
|
||||||
private int limit;
|
private int limit;
|
||||||
|
|
||||||
public static ObjectPool<JsonReader> createPool(int max) {
|
// public static ObjectPool<JsonReader> createPool(int max) {
|
||||||
return new ObjectPool<>(max, (Object... params) -> new JsonReader(), null, JsonReader::recycle);
|
// return new ObjectPool<>(max, (Object... params) -> new JsonReader(), null, JsonReader::recycle);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public JsonReader() {
|
public JsonReader() {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,9 +32,9 @@ public class JsonWriter extends Writer {
|
|||||||
|
|
||||||
protected boolean tiny;
|
protected boolean tiny;
|
||||||
|
|
||||||
public static ObjectPool<JsonWriter> createPool(int max) {
|
// public static ObjectPool<JsonWriter> createPool(int max) {
|
||||||
return new ObjectPool<>(max, (Object... params) -> new JsonWriter(), null, JsonWriter::recycle);
|
// return new ObjectPool<>(max, (Object... params) -> new JsonWriter(), null, JsonWriter::recycle);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public JsonWriter() {
|
public JsonWriter() {
|
||||||
this(defaultSize);
|
this(defaultSize);
|
||||||
|
|||||||
Reference in New Issue
Block a user