Skip to content

Commit 6086904

Browse files
authored
Merge pull request #620 from cyberdelia/shorthand
Add an ObjectMapper shorthand
2 parents fba05ce + 2f71555 commit 6086904

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

msgpack-jackson/README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,17 @@ Only thing you need to do is to instantiate `MessagePackFactory` and pass it to
5858
System.out.println(deserialized.getName()); // => komamitsu
5959
```
6060

61+
Or more easily:
62+
63+
```java
64+
ObjectMapper objectMapper = new MessagePackMapper();
65+
```
66+
6167
### Serialization/Deserialization of List
6268

6369
```java
6470
// Instantiate ObjectMapper for MessagePack
65-
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
71+
ObjectMapper objectMapper = new MessagePackMapper();
6672

6773
// Serialize a List to byte array
6874
List<Object> list = new ArrayList<>();
@@ -80,7 +86,7 @@ Only thing you need to do is to instantiate `MessagePackFactory` and pass it to
8086

8187
```java
8288
// Instantiate ObjectMapper for MessagePack
83-
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
89+
ObjectMapper objectMapper = MessagePackMapper();
8490

8591
// Serialize a Map to byte array
8692
Map<String, Object> map = new HashMap<>();
@@ -146,7 +152,7 @@ On the other hand, jackson-databind serializes and deserializes a POJO as a key-
146152
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:
147153

148154
```java
149-
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
155+
ObjectMapper objectMapper = new MessagePackMapper();
150156
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
151157
```
152158

@@ -156,7 +162,7 @@ But if you want to make this library handle POJOs in the same way as msgpack-jav
156162

157163
```java
158164
OutputStream out = new FileOutputStream(tempFile);
159-
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
165+
ObjectMapper objectMapper = new MessagePackMapper();
160166
objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
161167

162168
objectMapper.writeValue(out, 1);
@@ -181,7 +187,7 @@ But if you want to make this library handle POJOs in the same way as msgpack-jav
181187
packer.close();
182188

183189
FileInputStream in = new FileInputStream(tempFile);
184-
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
190+
ObjectMapper objectMapper = new MessagePackMapper();
185191
objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
186192
System.out.println(objectMapper.readValue(in, Integer.class));
187193
System.out.println(objectMapper.readValue(in, String.class));
@@ -194,7 +200,7 @@ Old msgpack-java (e.g 0.6.7) doesn't support MessagePack str8 type. When your ap
194200

195201
```java
196202
MessagePack.PackerConfig config = new MessagePack.PackerConfig().withStr8FormatSupport(false);
197-
ObjectMapper mapperWithConfig = new ObjectMapper(new MessagePackFactory(config));
203+
ObjectMapper mapperWithConfig = new MessagePackMapper(new MessagePackFactory(config));
198204
// This string is serialized as bin8 type
199205
byte[] resultWithoutStr8Format = mapperWithConfig.writeValueAsBytes(str8LengthString);
200206
```
@@ -211,7 +217,7 @@ When you want to use non-String value as a key of Map, use `MessagePackKeySerial
211217

212218
intMap.put(42, "Hello");
213219

214-
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
220+
ObjectMapper objectMapper = new MessagePackMapper();
215221
byte[] bytes = objectMapper.writeValueAsBytes(intMap);
216222

217223
Map<Integer, String> deserialized = objectMapper.readValue(bytes, new TypeReference<Map<Integer, String>>() {});
@@ -407,15 +413,15 @@ When you serialize an object that has a nested object also serializing with Obje
407413
@Test
408414
public void testNestedSerialization() throws Exception
409415
{
410-
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
416+
ObjectMapper objectMapper = new MessagePackMapper();
411417
objectMapper.writeValueAsBytes(new OuterClass());
412418
}
413419

414420
public class OuterClass
415421
{
416422
public String getInner() throws JsonProcessingException
417423
{
418-
ObjectMapper m = new ObjectMapper(new MessagePackFactory());
424+
ObjectMapper m = new MessagePackMapper();
419425
m.writeValueAsBytes(new InnerClass());
420426
return "EFG";
421427
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.jackson.dataformat;
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
20+
21+
public class MessagePackMapper extends ObjectMapper
22+
{
23+
private static final long serialVersionUID = 3L;
24+
25+
public static class Builder extends MapperBuilder<MessagePackMapper, Builder>
26+
{
27+
public Builder(MessagePackMapper m)
28+
{
29+
super(m);
30+
}
31+
}
32+
33+
public MessagePackMapper()
34+
{
35+
this(new MessagePackFactory());
36+
}
37+
38+
public MessagePackMapper(MessagePackFactory f)
39+
{
40+
super(f);
41+
}
42+
43+
public static Builder builder()
44+
{
45+
return new Builder(new MessagePackMapper());
46+
}
47+
48+
public static Builder builder(MessagePackFactory f)
49+
{
50+
return new Builder(new MessagePackMapper(f));
51+
}
52+
}

0 commit comments

Comments
 (0)