Skip to content

Commit db4fe9d

Browse files
committed
adding helper for serialization format of object mapper to be array
1 parent dfdbc5b commit db4fe9d

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
* @author marenzo
17+
*/
18+
public class JsonArrayFormat extends JacksonAnnotationIntrospector {
19+
20+
private final static JsonFormat.Value ARRAY_FORMAT = new JsonFormat.Value().withShape(ARRAY);
21+
22+
@Override
23+
public JsonFormat.Value findFormat(Annotated ann)
24+
{
25+
// If the entity contains JsonFormat annotation, give it higher priority.
26+
JsonFormat.Value precedenceFormat = super.findFormat(ann);
27+
if (precedenceFormat != null) {
28+
return precedenceFormat;
29+
}
30+
31+
return ARRAY_FORMAT;
32+
}
33+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +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;
22+
import java.nio.charset.Charset;
2123
import java.util.Arrays;
2224

25+
import static org.hamcrest.CoreMatchers.not;
26+
import static org.hamcrest.CoreMatchers.containsString;
27+
2328
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertThat;
2430
import static org.junit.Assert.assertTrue;
2531

2632
public class MessagePackDataformatForPojoTest
@@ -99,4 +105,18 @@ public void testChangingPropertyNames()
99105
ChangingPropertyNamesPojo value = objectMapper.readValue(bytes, ChangingPropertyNamesPojo.class);
100106
assertEquals("komamitsu", value.getTheName());
101107
}
108+
109+
@Test
110+
public void testSerializationWithoutSchema()
111+
throws IOException
112+
{
113+
ObjectMapper objectMapper = new ObjectMapper(factory); // to not affect shared objectMapper state
114+
UsingCustomConstructorPojo orig = new UsingCustomConstructorPojo("komamitsu", 55);
115+
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
116+
byte[] bytes = objectMapper.writeValueAsBytes(orig);
117+
String scheme = new String(bytes, Charset.forName("UTF-8"));
118+
assertThat(scheme, not(containsString("name")));
119+
UsingCustomConstructorPojo value = objectMapper.readValue(bytes, UsingCustomConstructorPojo.class);
120+
assertEquals("komamitsu", value.name);
121+
}
102122
}

0 commit comments

Comments
 (0)