MessagePack Jackson 数据大小

简介: 我们在使用 MessagePack 对 List 对象数据进行序列化的时候,发现序列化以后的二进制数组数据偏大的情况。请注意,不是所有的 List 对象都会出现这种情况,这个根据你 List 对象中存储的内容有关。

我们在使用 MessagePack 对 List 对象数据进行序列化的时候,发现序列化以后的二进制数组数据偏大的情况。

请注意,不是所有的 List 对象都会出现这种情况,这个根据你 List 对象中存储的内容有关。

有关本问题的测试源代码请参考:https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/MessagePackDataTest.java 中的内容。

考察下面的代码:

List<MessageData> dataList = MockDataUtils.getMessageDataList(600000);
 
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
raw = objectMapper.writeValueAsBytes(dataList);
 
FileUtils.byteCountToDisplaySize(raw.length);
logger.debug("Raw Size: [{}]", FileUtils.byteCountToDisplaySize(raw.length));

我们会发现,针对这个 60 万个对象的 List 的序列化后的数据达到了 33MB。

如果我们再定义  ObjectMapper 对象的时候添加一部分参数,我们会发现大小将会有显著改善。

请参考下面的代码:

List<MessageData> dataList = MockDataUtils.getMessageDataList(600000);
 
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
 
rawJsonArray = objectMapper.writeValueAsBytes(dataList);
logger.debug("rawJsonArray Size: [{}]", FileUtils.byteCountToDisplaySize(rawJsonArray.length));

如果你运行上面的代码,你会看到程序的输出字符串将会降低到 23MB。

这里面主要是 objectMapper.setAnnotationIntrospector(new JsonArrayFormat()); 这句话起了作用。

在正常的场景中,我们可以通过 注解 JsonIgnore, 将其加到属性上,即解析时即会过滤到属性。

而实际实现,则是由类 JacksonAnnotationIntrospector 中 的 hasIgnoreMarker 来完成,则就是通过读取注解来判断属性是否应该被exclude掉。ObjectMapper中默认的 AnnotationIntrospector 即是 JacksonAnnotationIntrospector 来完成,但我们可以通过 方法 ObjectMapper.setAnnotationIntrospector 来重新指定自定义的实现。

https://www.cwiki.us/display/Serialization/MessagePack+Jackson+Data+Size

目录
相关文章
|
9月前
|
JSON 测试技术 数据格式
MessagePack 和System.Text.Json 序列化和反序列化对比
MessagePack 和System.Text.Json 序列化和反序列化对比
91 0
MessagePack 和System.Text.Json 序列化和反序列化对比
|
10月前
|
资源调度 JavaScript 开发者
关于项目中的package.json的介绍
关于项目中的package.json的介绍
93 0
|
10月前
|
存储 JSON 安全
msgpack,fmtlib和RPClib库的介绍及使用
msgpack,fmtlib和RPClib库的介绍及使用
|
前端开发 API 开发者
乱花迷人眼 - 一文彻底看懂 package.json 中的各种 dependencies
package.json 中存在各种各样的依赖定义:dependencies、devDependencies、peerDependencies、optionalDependencies、bundleDependencies,很容易让初学的开发者晕头,到底有什么区别。
|
10月前
|
前端开发
package.json 中的配置
package.json 中的配置
83 0
|
存储 JSON 缓存
MessagePack - 简介及使用
MessagePack - 简介及使用
699 1
MessagePack - 简介及使用
|
JSON Java 数据格式
json进阶---jackson底层之JsonParser理解使用
json进阶---jackson底层之JsonParser理解使用(springboot多结构参数的映射方法的实现思路)
json进阶---jackson底层之JsonParser理解使用
|
JSON 前端开发 Java
【Json与Ajax交互报错解决】No converter found for return value of type: class com.github.pagehelper.PageInfo
【Json与Ajax交互报错解决】No converter found for return value of type: class com.github.pagehelper.PageInfo
267 0
【Json与Ajax交互报错解决】No converter found for return value of type: class com.github.pagehelper.PageInfo
package.json中dependenices 和 devDependenices的区别
package.json中dependenices 和 devDependenices的区别