From fac57035ff7637eb31e142fe27267314ae399ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E5=B9=B3=E7=BA=BF?= <22250530@qq.com> Date: Fri, 6 Nov 2015 12:46:36 +0800 Subject: [PATCH] --- .../redkale/convert/json/JsonConvert.java | 31 ++++++++++++++++--- .../redkale/convert/json/JsonWriter.java | 6 ++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/com/wentch/redkale/convert/json/JsonConvert.java b/src/com/wentch/redkale/convert/json/JsonConvert.java index d965935e3..7bc3579eb 100644 --- a/src/com/wentch/redkale/convert/json/JsonConvert.java +++ b/src/com/wentch/redkale/convert/json/JsonConvert.java @@ -30,6 +30,14 @@ public final class JsonConvert extends Convert { 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 { 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 { 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 { 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); diff --git a/src/com/wentch/redkale/convert/json/JsonWriter.java b/src/com/wentch/redkale/convert/json/JsonWriter.java index bbd5db556..ec67e5cd8 100644 --- a/src/com/wentch/redkale/convert/json/JsonWriter.java +++ b/src/com/wentch/redkale/convert/json/JsonWriter.java @@ -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; } //----------------------------------------------------------------------- //----------------------------------------------------------------------- - /** * 返回指定至少指定长度的缓冲区 *