From 370d9fab042856bc8f562092087f6d92159b3653 Mon Sep 17 00:00:00 2001 From: wentch <22250530@qq.com> Date: Thu, 31 Dec 2015 17:22:05 +0800 Subject: [PATCH] --- convert.html | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/convert.html b/convert.html index a97084d96..609583513 100644 --- a/convert.html +++ b/convert.html @@ -56,7 +56,21 @@ public <T> T convertFrom(final Type type, final char[] text, int start, int len); public <T> T convertFrom(final Type type, final ByteBuffer... buffers); -
基本用法:
+ +Convert 与 ByteBuffer 的结合
+从以上的方法可以看出,与其他JSON框架相比Convert多了与ByteBuffer结合的方法。特别是convertTo方法加了Supplier<ByteBuffer>方法,这么做是为了提高数据传输的性能。在大部分情况下JSON序列化得到的数据流是为了传输出去,常见的场景就是HTTP+JSON接口。Convert提供ByteBuffer接口会大量减少中间临时数据的产生。通常大家输出JSON数据的方法如下: +
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ String json = new Gson().toJson(record);
+ resp.setContentType("text/json; charset=UTF-8");
+ resp.getOutputStream().write(json.getBytes("UTF-8"));
+ }几乎所有的JSON框架提供的接口以String作为返回结果为主,其内在都是以char[]作为JsonWriter的载体。以Gson为例,Gson拼接JSON默认使用的是StringWriter,StringWriter的扩容策略是翻倍。为了方便计算,假设一个对象转换成JSON字符串大小为了10K。Gson在转换过程中产生的临时的char[]的大小: 16 + 32 + 64 + 128 + 256 + 512 + 1K + 2K + 4K + 8K + 16K = 32K, char[]转换成最终的String结果又会产生10K的char[], 最后在response输出时又回产生10K的byte[](方便计算不考虑双字节),也就是说整个对象输出过程中会产生52K的临时数据。同时常见的HTTP服务器(如实现java-servlet规范的服务器)不会把底层的ByteBuffer对象池暴露给上层。所以目前所有使用其他JSON框架输出数据都会产生5倍于数据体积大小的垃圾数据。而RedKale框架的HTTP服务内置了Convert的JSON接口,避免了这么大量的垃圾数据产生。
+ protected void execute(HttpRequest req, HttpResponse resp) throws IOException {
+ resp.finishJson(record);
+ }JSON基本用法:
public class UserRecord {
private int userid;
@@ -153,8 +167,8 @@
JsonFactory.root().register(java.io.File.class, FileSimpleCoder.instance);
BsonFactory.root().register(java.io.File.class, FileSimpleCoder.instance);
- BSON类似Java自带的Serializable, 其格式如下:
1). 基本数据类型: 直接转换成byte[]