Skip to content

Commit 9aa99f6

Browse files
committed
Merge pull request msgpack#358 from marenzo/array_format_support
adding helper for serialization format of object mapper to be array
2 parents 202ca62 + da2f54a commit 9aa99f6

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

msgpack-jackson/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ Java
8383
// xs => [zero, 1, 2.0, null]
8484
```
8585

86+
### Serialization format
87+
88+
By default, the serialization format is object, which means it includes the schema of the serialized entity (POJO).
89+
To serialize an entity without the schema, only as array, you can add the annotation `@JsonFormat(shape=JsonFormat.Shape.ARRAY)` to the entity definition.
90+
Also, it's possible to set the serialization format for the object mapper instance to be array by changing the annotation inspector of object mapper to `JsonArrayFormat`:
91+
92+
```
93+
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
94+
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
95+
```
96+
97+
This format provides compatibility with msgpack-java 0.6.x serialization api.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.msgpack.jackson.dataformat;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.databind.introspect.Annotated;
5+
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
6+
7+
import static com.fasterxml.jackson.annotation.JsonFormat.Shape.ARRAY;
8+
9+
/**
10+
* Provides the ability of serializing POJOs without their schema.
11+
* Similar to @JsonFormat annotation with JsonFormat.Shape.ARRAY, but in a programmatic
12+
* way.
13+
*
14+
* This also provides same behavior as msgpack-java 0.6.x serialization api.
15+
*/
16+
public class JsonArrayFormat extends JacksonAnnotationIntrospector
17+
{
18+
private static final JsonFormat.Value ARRAY_FORMAT = new JsonFormat.Value().withShape(ARRAY);
19+
20+
@Override
21+
public JsonFormat.Value findFormat(Annotated ann)
22+
{
23+
// If the entity contains JsonFormat annotation, give it higher priority.
24+
JsonFormat.Value precedenceFormat = super.findFormat(ann);
25+
if (precedenceFormat != null) {
26+
return precedenceFormat;
27+
}
28+
29+
return ARRAY_FORMAT;
30+
}
31+
}

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatForPojoTest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
//
1616
package org.msgpack.jackson.dataformat;
1717

18+
import com.fasterxml.jackson.databind.ObjectMapper;
1819
import org.junit.Test;
1920

2021
import java.io.IOException;
21-
import java.util.Arrays;
22+
import java.nio.charset.Charset;
23+
24+
import static org.hamcrest.CoreMatchers.not;
25+
import static org.hamcrest.CoreMatchers.containsString;
2226

2327
import static org.junit.Assert.assertEquals;
24-
import static org.junit.Assert.assertTrue;
28+
import static org.junit.Assert.assertThat;
29+
import static org.junit.Assert.assertArrayEquals;
2530

2631
public class MessagePackDataformatForPojoTest
2732
extends MessagePackDataformatTestBase
@@ -38,7 +43,7 @@ public void testNormal()
3843
assertEquals(normalPojo.l, value.l);
3944
assertEquals(normalPojo.f, value.f, 0.000001f);
4045
assertEquals(normalPojo.d, value.d, 0.000001f);
41-
assertTrue(Arrays.equals(normalPojo.b, value.b));
46+
assertArrayEquals(normalPojo.b, value.b);
4247
assertEquals(normalPojo.bi, value.bi);
4348
assertEquals(normalPojo.suit, Suit.HEART);
4449
}
@@ -50,7 +55,7 @@ public void testNestedList()
5055
byte[] bytes = objectMapper.writeValueAsBytes(nestedListPojo);
5156
NestedListPojo value = objectMapper.readValue(bytes, NestedListPojo.class);
5257
assertEquals(nestedListPojo.s, value.s);
53-
assertTrue(Arrays.equals(nestedListPojo.strs.toArray(), value.strs.toArray()));
58+
assertArrayEquals(nestedListPojo.strs.toArray(), value.strs.toArray());
5459
}
5560

5661
@Test
@@ -99,4 +104,20 @@ public void testChangingPropertyNames()
99104
ChangingPropertyNamesPojo value = objectMapper.readValue(bytes, ChangingPropertyNamesPojo.class);
100105
assertEquals("komamitsu", value.getTheName());
101106
}
107+
108+
@Test
109+
public void testSerializationWithoutSchema()
110+
throws IOException
111+
{
112+
ObjectMapper objectMapper = new ObjectMapper(factory); // to not affect shared objectMapper state
113+
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
114+
byte[] bytes = objectMapper.writeValueAsBytes(complexPojo);
115+
String scheme = new String(bytes, Charset.forName("UTF-8"));
116+
assertThat(scheme, not(containsString("name"))); // validating schema doesn't contains keys, that's just array
117+
ComplexPojo value = objectMapper.readValue(bytes, ComplexPojo.class);
118+
assertEquals("komamitsu", value.name);
119+
assertEquals(20, value.age);
120+
assertArrayEquals(complexPojo.values.toArray(), value.values.toArray());
121+
assertEquals(complexPojo.grades.get("math"), value.grades.get("math"));
122+
}
102123
}

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatTestBase.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.util.ArrayList;
3333
import java.util.Arrays;
3434
import java.util.List;
35+
import java.util.Map;
36+
import java.util.Collections;
3537

3638
public class MessagePackDataformatTestBase
3739
{
@@ -43,6 +45,7 @@ public class MessagePackDataformatTestBase
4345
protected NestedListPojo nestedListPojo;
4446
protected NestedListComplexPojo nestedListComplexPojo;
4547
protected TinyPojo tinyPojo;
48+
protected ComplexPojo complexPojo;
4649

4750
@Before
4851
public void setup()
@@ -65,14 +68,21 @@ public void setup()
6568

6669
nestedListPojo = new NestedListPojo();
6770
nestedListPojo.s = "a string";
68-
nestedListPojo.strs = Arrays.asList(new String[] {"string", "another string", "another string"});
71+
nestedListPojo.strs = Arrays.asList("string", "another string", "another string");
6972

7073
tinyPojo = new TinyPojo();
7174
tinyPojo.t = "t string";
75+
7276
nestedListComplexPojo = new NestedListComplexPojo();
7377
nestedListComplexPojo.s = "a string";
7478
nestedListComplexPojo.foos = new ArrayList<TinyPojo>();
7579
nestedListComplexPojo.foos.add(tinyPojo);
80+
81+
complexPojo = new ComplexPojo();
82+
complexPojo.name = "komamitsu";
83+
complexPojo.age = 20;
84+
complexPojo.grades = Collections.singletonMap("math", 97);
85+
complexPojo.values = Arrays.asList("one", "two", "three");
7686
}
7787

7888
@After
@@ -108,6 +118,14 @@ public static class NestedListPojo
108118
public List<String> strs;
109119
}
110120

121+
public static class ComplexPojo
122+
{
123+
public String name;
124+
public int age;
125+
public List<String> values;
126+
public Map<String, Integer> grades;
127+
}
128+
111129
public static class TinyPojo
112130
{
113131
public String t;

0 commit comments

Comments
 (0)