Java的序列化与反序列化工具 Redkale-Convert

码农软件 · 软件分类 · 常用工具包 · 2019-08-14 23:28:37

软件介绍

Convert 是一个比较独立的组件,仅依赖于util包。提供Java对象的序列化与反序列化功能。支持JSON(JavaScript Object Notation)、BSON(Binary Stream Object Notation)两种格式化。 两种格式使用方式完全一样,其性能都大幅度超过其他JSON框架。同时JSON内置于HTTP服务中,BSON也是SNCP协议数据序列化的基础。

Convert 快速上手

本介绍仅以JSON为例(BSON与JSON使用方式雷同)。其操作类主要是JsonConvert,配置类主要是JsonFactory、ConvertColumn。JsonFactory采用同ClassLoader类似的双亲委托方式设计。

JsonConvert 序列化encode方法:

public String convertTo(final Object value); 

public String convertTo(final Type type, final Object value);  

public void convertTo(final OutputStream out, final Object value);  

public void convertTo(final OutputStream out, final Type type, final Object value);  

public ByteBuffer[] convertTo(Supplier<ByteBuffer> supplier, final Object value);  

public ByteBuffer[] convertTo(Supplier<ByteBuffer> supplier, Type type, Object value);  

public void convertTo(final JsonWriter writer, final Object value);  

public void convertTo(final JsonWriter writer, final Type type, final Object value);

JsonConvert 反序列化decode方法:

public <T> T convertFrom(final Type type, final String text);  

public <T> T convertFrom(final Type type, final char[] text);  

public <T> T convertFrom(final Type type, final char[] text, int start, int len);  

public <T> T convertFrom(final Type type, final InputStream in);  

public <T> T convertFrom(final Type type, final ByteBuffer... buffers);  

public <T> T convertFrom(final Type type, final JsonReader reader);

Convert 与 ByteBuffer 的结合

  从以上的方法可以看出,与其他JSON框架相比Convert多了与ByteBuffer结合的方法。特别是convertTo方法加了Supplier<ByteBuffer>方法,这么做是为了提高数据传输的性能。在大部分情况下JSON序列化得到的数据流是为了传输出去,常见的场景就是HTTP+JSON接口。Convert提供ByteBuffer接口会大量减少中间临时数据的产生。大部分输出JSON数据的方法如下:

public void doPost(HttpRequest req, HttpResponse resp) throws ServletException {  
    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对象池暴露给上层。所以以String为输出结果的JSON方法都会产生5倍于数据体积大小(其他低于1倍扩容策略的框架会产生更多)的垃圾数据。
   Redkale框架的HTTP服务内置了Convert的JSON接口,避免了大量的垃圾数据产生。Redkale的HTTP是基于AIO(NIO.2)实现且存在ByteBuffer对象池,response的finishJson系列方法将HTTP服务的ByteBuffer对象池传给Convert, 使Convert在序列化过程中直接以UTF-8编码方式输出到ByteBuffer里,输出结束后将ByteBuffer交给对象池回收,从而减少大量构建bye[]、char[]所产生的临时数据。

protected void execute(HttpRequest req, HttpResponse resp) throws IOException {  
    resp.finishJson(record); 
}


详情见: http://redkale.org/convert.html


本文地址:https://codercto.com/soft/d/12386.html

Web Security Testing Cookbook

Web Security Testing Cookbook

Paco Hope、Ben Walther / O'Reilly Media / 2008-10-24 / USD 39.99

Among the tests you perform on web applications, security testing is perhaps the most important, yet it's often the most neglected. The recipes in the Web Security Testing Cookbook demonstrate how dev......一起来看看 《Web Security Testing Cookbook》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器