When we use MessagePack of the List object to serialize the data and found that the case of binary array data after serialization too large.
Please note that not all of the List object will happen, according to the related content stored in your List object.
About this issue test source code, please refer to: https://github.com/cwiki-us-demo/serialize-deserialize-demo-java/blob/master/src/test/java/com/insight/demo/serialize/ MessagePackDataTest.java content.
Consider the following code:
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));
We will find this data for serialization List of 600 000 objects reached 33MB.
If we define the object ObjectMapper time to add some of the parameters, we find that the size will be significantly improved.
Please refer to the following code:
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));
If you run the above code, you will see the output strings program will be reduced to 23MB.
This is mainly inside objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
this sentence played a role.
In the normal scenario, we can annotation JsonIgnore, given on a property, i.e., a filter that is to be parsed attributes.
The actual implementation of the class is JacksonAnnotationIntrospector
the hasIgnoreMarker
accomplished by reading the annotation is to be determined whether the attribute should exclude off. ObjectMapper
The default AnnotationIntrospector
that is JacksonAnnotationIntrospector
done, but we can by methods ObjectMapper.setAnnotationIntrospector
reassign implement custom.
https://www.cwiki.us/display/Serialization/MessagePack+Jackson+Data+Size