# jackson-dataformat-msgpack
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.msgpack/jackson-dataformat-msgpack/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.msgpack/jackson-dataformat-msgpack/)
[![Javadoc](https://www.javadoc.io/badge/org.msgpack/jackson-dataformat-msgpack.svg)](https://www.javadoc.io/doc/org.msgpack/jackson-dataformat-msgpack)
This Jackson extension library is a component to easily read and write [MessagePack](http://msgpack.org/) 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.
```java
// 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:
```java
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](#serialize-and-deserialize-bigdecimal-as-str-type-internally-in-messagepack-format) for details.
```java
ObjectMapper objectMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString();
```
### Serialization/Deserialization of List
```java
// Instantiate ObjectMapper for MessagePack
ObjectMapper objectMapper = new MessagePackMapper();
// Serialize a List to byte array
List<Object> list = new ArrayList<>();
list.add("Foo");
list.add("Bar");
list.add(42);
byte[] bytes = objectMapper.writeValueAsBytes(list);
// Deserialize the byte array to a List
List<Object> deserialized = objectMapper.readValue(bytes, new TypeReference<List<Object>>() {});
System.out.println(deserialized); // => [Foo, Bar, 42]
```
### Serialization/Deserialization of Map
```java
// Instantiate ObjectMapper for MessagePack
ObjectMapper objectMapper = MessagePackMapper();
// Serialize a Map to byte array
Map<String, Object> map = new HashMap<>();
map.put("name", "komamitsu");
map.put("age", 42);
byte[] bytes = objectMapper.writeValueAsBytes(map);
// Deserialize the byte array to a Map
Map<String, Object> deserialized = objectMapper.readValue(bytes, new TypeReference<Map<String, Object>>() {});
System.out.println(deserialized); // => {name=komamitsu, age=42}
```
### Example of Serialization/Deserialization over multiple languages
Java
```java
// Serialize
Map<String, Object> obj = new HashMap<String, Object>();
obj.put("foo", "hello");
obj.put("bar", "world");
byte[] bs = objectMapper.writeValueAsBytes(obj);
// bs => [-126, -93, 102, 111, 111, -91, 104, 101, 108, 108, 111,
// -93, 98, 97, 114, -91, 119, 111, 114, 108, 100]
```
Ruby
```ruby
require 'msgpack'
# Deserialize
xs = [-126, -93, 102, 111, 111, -91, 104, 101, 108, 108, 111,
-93, 98, 97, 114, -91, 119, 111, 114, 108, 100]
MessagePack.unpack(xs.pack("C*"))
# => {"foo"=>"hello", "bar"=>"world"}
# Serialize
["zero", 1, 2.0, nil].to_msgpack.unpack('C*')
# => [148, 164, 122, 101, 114, 111, 1, 203, 64, 0, 0, 0, 0, 0, 0, 0, 192]
```
Java
```java
// Deserialize
bs = new byte[] {(byte) 148, (byte) 164, 122, 101, 114, 111, 1,
(byte) 203, 64, 0, 0, 0, 0, 0, 0, 0, (byte) 192};
TypeReference<List<Object>> typeReference = new TypeReference<List<Object>>(){};
List<Object> xs = objectMapper.readValue(bs, typeReference);
// xs => [zero, 1, 2.0, null]
```
## Advanced usage
### Serialize/Deserialize POJO as MessagePack array type to keep compatibility with msgpack-java:0.6
In msgpack-java:0.6 or earlier, a POJO was serliazed and deserialized as an array of values in MessagePack format. The order of values depended on an internal order of Java class's variables and it was a naive way and caused some issues since Java class's variables order isn't guaranteed over Java implementations.
On the other hand, jackson-databind serializes and deserializes a POJO as a key-value object. So this `jackson-dataformat-msgpack` also handles POJOs in the same way. As a result, it isn't compatible with msgpack-java:0.6 or earlier in serialization and deserialization of POJOs.
But if you want to make this library handle POJOs in the same way as msgpack-java:0.6 or earlier, you can use `JsonArrayFormat` like this:
```java
ObjectMapper objectMapper = new MessagePackMapper();
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
```
### Serialize multiple values without closing an output stream
`com.fasterxml.jackson.databind.ObjectMapper` closes an output stream by default after it writes a value. If you want to serialize multiple values in a row without closing an output stream, set `JsonGenerator.Feature.AUTO_CLOSE_TARGET` to false.
```java
OutputStream out = new FileOutputStream(tempFile);
ObjectMapper objectMapper = new MessagePackMapper();
objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
objectMapper.writeValue(out, 1);
objectMapper.writeValue(out, "two");
objectMapper.writeValue(out, 3.14);
out.close();
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(new FileInputStream(tempFile));
System.out.println(unpacker.unpackInt()); // => 1
System.out.println(unpacker.unpackString()); // => two
System.out.println(unpacker.unpackFloat()); // => 3.14
```
### Deserialize multiple values without closing an input stream
`com.fasterxml.jackson.databind.ObjectMapper` closes an input stream by default after it reads a value. If you want to deserialize multiple values in a row without closing an output stream, set `JsonParser.Feature.AUTO_CLOSE_SOURCE` to false.
```java
MessagePacker packer = MessagePack.newDefaultPacker(new FileOutputStream(tempFile));
packer.packInt(42);
packer.packString("Hello");
packer.close();
FileInputStream in = new FileInputStream(tempFile);
ObjectMapper objectMapper = new MessagePackMapper();
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
System.out.println(objectMapper.readValue(in, Integer.class));
System.out.println(objectMapper.readValue(in, String.class));
in.close();
```
### Serialize not using str8 type
Old msgpack-java (e.g 0.6.7) doesn't support MessagePack str8 type. When your application needs to comunicate with such an old MessagePack library, you can disable the data type like this:
```java
MessagePack.PackerConfig config = new MessagePack.PackerConfig().withStr8Forma
没有合适的资源?快使用搜索试试~ 我知道了~
msgpack源码 MessagePack高效的二进制序列化格式
共141个文件
java:98个
scala:19个
yml:8个
需积分: 5 0 下载量 35 浏览量
2023-11-22
14:14:38
上传
评论
收藏 235KB ZIP 举报
温馨提示
MessagePack,类似于JSON的二进制序列化格式,兼具速度和效率。它在存储时对数字、多字节字符、数组等都做了很多优化,减少了无用的字符,采用二进制格式,避免了字符化带来的额外存储空间的增加。由于这些优化,MessagePack比JSON更小更快。例如,小整数被编码为一个字节,典型的短字符串除字符串本身外仅需要一个额外的字节。此外,Msgpack还能在多种语言之间交换数据,增加了它的实用性。这种高效的序列化方式使得MessagePack在很多场景中都能取代JSON。
资源推荐
资源详情
资源评论
收起资源包目录
msgpack源码 MessagePack高效的二进制序列化格式 (141个子文件)
AUTHORS 81B
.scalafmt.conf 73B
.gitignore 158B
MessageUnpacker.java 62KB
MessagePackParserTest.java 40KB
MessagePacker.java 39KB
MessagePackGeneratorTest.java 34KB
Variable.java 31KB
MessagePack.java 28KB
MessagePackParser.java 22KB
MessageBuffer.java 22KB
Preconditions.java 19KB
MessagePackGenerator.java 18KB
Value.java 16KB
DirectBufferAccess.java 11KB
MessagePackExample.java 10KB
ValueFactory.java 9KB
ImmutableMapValueImpl.java 8KB
TimestampExtensionModuleTest.java 8KB
MessagePackDataformatTestBase.java 7KB
MessagePackFactoryTest.java 6KB
MessagePackDataformatPojoBenchmarkTest.java 6KB
ImmutableBigIntegerValueImpl.java 6KB
ImmutableArrayValueImpl.java 6KB
MessageBufferU.java 6KB
ExampleOfTypeInformationSerDe.java 6KB
MessageFormat.java 6KB
AbstractImmutableRawValue.java 5KB
MessagePackFactory.java 5KB
ImmutableLongValueImpl.java 5KB
ImmutableTimestampValueImpl.java 5KB
MessagePackDataformatForPojoTest.java 5KB
MessagePackDataformatHugeDataBenchmarkTest.java 5KB
ArrayBufferOutput.java 5KB
MessageBufferPacker.java 4KB
AbstractImmutableValue.java 4KB
MessagePackMapperTest.java 4KB
MessageBufferOutput.java 3KB
Benchmarker.java 3KB
ImmutableExtensionValueImpl.java 3KB
ImmutableDoubleValueImpl.java 3KB
TimestampExtensionModule.java 3KB
MessageBufferBE.java 3KB
ChannelBufferOutput.java 3KB
ImmutableBooleanValueImpl.java 3KB
IntegerValue.java 3KB
MessagePackSerializedString.java 3KB
ValueType.java 3KB
ImmutableStringValueImpl.java 2KB
ExtensionTypeHeader.java 2KB
OutputStreamBufferOutput.java 2KB
ImmutableBinaryValueImpl.java 2KB
InputStreamBufferInput.java 2KB
ArrayBufferInput.java 2KB
ChannelBufferInput.java 2KB
ImmutableNilValueImpl.java 2KB
SequenceMessageBufferInput.java 2KB
MessagePackMapper.java 2KB
NumberValue.java 2KB
MessageBufferInput.java 2KB
MessagePackExtensionType.java 2KB
RawValue.java 2KB
JsonArrayFormat.java 2KB
ByteBufferInput.java 2KB
ExtensionTypeCustomDeserializers.java 2KB
MessagePackSerializerFactory.java 2KB
MessageIntegerOverflowException.java 2KB
ArrayValue.java 2KB
MapValue.java 1KB
ImmutableValue.java 1KB
MessagePackException.java 1KB
MessagePackKeySerializer.java 1KB
ImmutableArrayValue.java 1KB
MessageStringCodingException.java 1KB
MessageInsufficientBufferException.java 1KB
StringValue.java 1KB
ExtensionValue.java 1KB
MessageTypeException.java 1KB
MessageSizeException.java 1KB
MessageNeverUsedFormatException.java 1KB
MessageTypeCastException.java 1KB
MessageFormatException.java 1KB
ImmutableNumberValue.java 1KB
ImmutableRawValue.java 1KB
ImmutableFloatValue.java 1016B
Tuple.java 1005B
FloatValue.java 967B
ImmutableBinaryValue.java 936B
BooleanValue.java 920B
TimestampValue.java 902B
ImmutableIntegerValue.java 893B
BinaryValue.java 886B
ImmutableStringValue.java 883B
ImmutableBooleanValue.java 876B
ImmutableMapValue.java 858B
ImmutableTimestampValue.java 848B
ImmutableExtensionValue.java 848B
ImmutableNilValue.java 786B
VisibleForTesting.java 754B
NilValue.java 748B
共 141 条
- 1
- 2
资源评论
不才hm
- 粉丝: 32
- 资源: 63
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 可直接运行 MATLAB数学建模学习资料 模拟算法MATLAB代码实现.rar
- 基于 Java+SQLServer 实现的医药售卖系统课程设计
- HCNP(HCDP)华为认证资深网络工程师-路由交换方向培训 -IESN中文理论书-内文.pdf
- 新版FPGA课程大纲,芯片硬件开发用的大纲
- ROS2下OpenCV识别物体区域和视频捕捉的样例
- STM32-EMBPI.PDF
- Font Awesome图标字体库提供可缩放矢量图标,它可以被定制大小、颜色、阴影以及任何可以用CSS的样式
- Bluefield 2固件镜像版本,fw-MBF2M345A-VENOT-ES-Ax-24.40.1000.bin
- 雪颜奇迹幻白双重莹白焕采霜50ML-1016-FA.rar
- Qt的QDOCK高级用法源码,包含linux和windows版本,从开源库下载
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功