Skip to content

Commit 8b75906

Browse files
committed
Add msgpack-jackson/README.md
1 parent d2b5faa commit 8b75906

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ For sbt users:
2121
libraryDependencies += "org.msgpack" % "msgpack-core" % "0.7.0"
2222
```
2323

24+
If you want to use MessagePack through [jackson-databind](https://github.com/FasterXML/jackson-databind), you can use *jackson-dataformat-msgpack*. Please read [README](msgpack-jackson/README.md) to know how to use it.
2425

2526
## For Developers
2627

msgpack-jackson/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# jackson-dataformat-msgpack
2+
3+
This Jackson extension library handles reading and writing of data encoded in [MessagePack](http://msgpack.org/) data format.
4+
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).
5+
6+
## Maven dependency
7+
8+
To use this module on Maven-based projects, use following dependency:
9+
10+
```
11+
<dependency>
12+
<groupId>org.msgpack</groupId>
13+
<artifactId>jackson-dataformat-msgpack</artifactId>
14+
<version>x.x.x</version>
15+
</dependency>
16+
```
17+
18+
## Usage
19+
20+
Only thing you need to do is to instantiate MessagePackFactory and pass it to the constructor of ObjectMapper.
21+
22+
```
23+
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
24+
ExamplePojo orig = new ExamplePojo("komamitsu");
25+
byte[] bytes = objectMapper.writeValueAsBytes(orig);
26+
ExamplePojo value = objectMapper.readValue(bytes, ExamplePojo.class);
27+
System.out.println(value.getName()); // => komamitsu
28+
```
29+
30+
Also, you can exchange data among multiple languages.
31+
32+
Java
33+
34+
```
35+
// Serialize
36+
Map<String, Object> obj = new HashMap<String, Object>();
37+
obj.put("foo", "hello");
38+
obj.put("bar", "world");
39+
byte[] bs = objectMapper.writeValueAsBytes(obj);
40+
// bs => [-126, -93, 102, 111, 111, -91, 104, 101, 108, 108, 111,
41+
// -93, 98, 97, 114, -91, 119, 111, 114, 108, 100]
42+
```
43+
44+
Ruby
45+
46+
```
47+
require 'msgpack'
48+
49+
# Deserialize
50+
xs = [-126, -93, 102, 111, 111, -91, 104, 101, 108, 108, 111,
51+
-93, 98, 97, 114, -91, 119, 111, 114, 108, 100]
52+
MessagePack.unpack(xs.pack("C*"))
53+
# => {"foo"=>"hello", "bar"=>"world"}
54+
55+
# Serialize
56+
["zero", 1, 2.0, nil].to_msgpack.unpack('C*')
57+
# => [148, 164, 122, 101, 114, 111, 1, 203, 64, 0, 0, 0, 0, 0, 0, 0, 192]
58+
```
59+
60+
Java
61+
62+
```
63+
// Deserialize
64+
bs = new byte[] {(byte) 148, (byte) 164, 122, 101, 114, 111, 1,
65+
(byte) 203, 64, 0, 0, 0, 0, 0, 0, 0, (byte) 192};
66+
TypeReference<List<Object>> typeReference = new TypeReference<List<Object>>(){};
67+
List<Object> xs = objectMapper.readValue(bs, typeReference);
68+
// xs => [zero, 1, 2.0, null]
69+
```
70+

0 commit comments

Comments
 (0)