GitHub

jackson-dataformat-msgpack

Maven Central Javadoc

This Jackson extension library is a component to easily read and write MessagePack encoded data through jackson-databind API.

It extends standard Jackson streaming API (JsonFactory, JsonParser, JsonGenerator), and as such works seamlessly with all the higher level data abstractions (data binding, tree model, and pluggable extensions). For the details of Jackson-annotations, please see https://github.com/FasterXML/jackson-annotations.

This library isn't compatible with msgpack-java v0.6 or earlier by default in serialization/deserialization of POJO. See Advanced usage below for details.

Install

Maven

<dependency>
  <groupId>org.msgpack</groupId>
  <artifactId>jackson-dataformat-msgpack</artifactId>
  <version>(version)</version>
</dependency>

Sbt

libraryDependencies += "org.msgpack" % "jackson-dataformat-msgpack" % "(version)"

Gradle

repositories {
    mavenCentral()
}
dependencies {
    compile 'org.msgpack:jackson-dataformat-msgpack:(version)'
}

Basic usage

Serialization/Deserialization of POJO

Only thing you need to do is to instantiate MessagePackFactory and pass it to the constructor of com.fasterxml.jackson.databind.ObjectMapper. And then, you can use it for MessagePack format data in the same way as jackson-databind.

  // Instantiate ObjectMapper for MessagePack
  ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
  // Serialize a Java object to byte array
  ExamplePojo pojo = new ExamplePojo("komamitsu");
  byte[] bytes = objectMapper.writeValueAsBytes(pojo);
  // Deserialize the byte array to a Java object
  ExamplePojo deserialized = objectMapper.readValue(bytes, ExamplePojo.class);
  System.out.println(deserialized.getName()); // => komamitsu

Or more easily:

  ObjectMapper objectMapper = new MessagePackMapper();

We strongly recommend to call MessagePackMapper#handleBigIntegerAndBigDecimalAsString() if you serialize and/or deserialize BigInteger/BigDecimal values. See Serialize and deserialize BigDecimal as str type internally in MessagePack format for details.

  ObjectMapper objectMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString();

Serialization/Deserialization of List

" // Instantiate ObjectMapper for MessagePack ObjectMapper objectMapper = new MessagePackMapper(); // Serialize a List to byte array List

Read the original on github.com ↗