This commit is contained in:
地平线
2015-11-06 12:46:36 +08:00
parent cd90512929
commit fac57035ff
2 changed files with 30 additions and 7 deletions

View File

@@ -30,6 +30,14 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
this.tiny = tiny;
}
public JsonWriter pollJsonWriter() {
return writerPool.poll().setTiny(tiny);
}
public void offerJsonWriter(JsonWriter out) {
if (out != null) writerPool.offer(out);
}
@Override
public JsonFactory getFactory() {
return (JsonFactory) factory;
@@ -57,8 +65,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
public String convertTo(final Type type, Object value) {
if (type == null) return null;
if (value == null) return "null";
final JsonWriter out = writerPool.poll();
out.setTiny(tiny);
final JsonWriter out = writerPool.poll().setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value);
String result = out.toString();
writerPool.offer(out);
@@ -70,6 +77,23 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
return convertTo(value.getClass(), value);
}
public void convertTo(final JsonWriter out, final Type type, Object value) {
if (type == null) return;
if (value == null) {
out.writeNull();
} else {
factory.loadEncoder(type).convertTo(out, value);
}
}
public void convertTo(final JsonWriter out, Object value) {
if (value == null) {
out.writeNull();
} else {
factory.loadEncoder(value.getClass()).convertTo(out, value);
}
}
public byte[] convertToUTF8Bytes(Object value) {
if (value == null) return new byte[]{110, 117, 108, 108};
return convertToUTF8Bytes(value.getClass(), value);
@@ -78,8 +102,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
public byte[] convertToUTF8Bytes(final Type type, Object value) {
if (type == null) return null;
if (value == null) return new byte[]{110, 117, 108, 108};
final JsonWriter out = writerPool.poll();
out.setTiny(tiny);
final JsonWriter out = writerPool.poll().setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value);
byte[] result = out.toUTF8Bytes();
writerPool.offer(out);

View File

@@ -14,7 +14,7 @@ import com.wentch.redkale.util.*;
*
* @author zhangjx
*/
public final class JsonWriter implements Writer {
public class JsonWriter implements Writer {
private static final char[] CHARS_TUREVALUE = "true".toCharArray();
@@ -45,13 +45,13 @@ public final class JsonWriter implements Writer {
return tiny;
}
public void setTiny(boolean tiny) {
public JsonWriter setTiny(boolean tiny) {
this.tiny = tiny;
return this;
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
/**
* 返回指定至少指定长度的缓冲区
*