Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions msgpack-jackson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ Java
// xs => [zero, 1, 2.0, null]
```

### Serialization format

By default, the serialization format is object, which means it includes the schema of the serialized entity (POJO).
To serialize an entity without the schema, only as array, you can add the annotation `@JsonFormat(shape=JsonFormat.Shape.ARRAY)` to the entity definition.
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`:

```
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
```

This format provides compatibility with msgpack-java 0.6.x serialization api.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.msgpack.jackson.dataformat;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;

import static com.fasterxml.jackson.annotation.JsonFormat.Shape.ARRAY;

/**
* Provides the ability of serializing POJOs without their schema.
* Similar to @JsonFormat annotation with JsonFormat.Shape.ARRAY, but in a programmatic
* way.
*
* This also provides same behavior as msgpack-java 0.6.x serialization api.
*
* @author marenzo
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this @author tag. msgpack-java should be owned by public community :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) removed

*/
public class JsonArrayFormat extends JacksonAnnotationIntrospector {

private final static JsonFormat.Value ARRAY_FORMAT = new JsonFormat.Value().withShape(ARRAY);

@Override
public JsonFormat.Value findFormat(Annotated ann)
{
// If the entity contains JsonFormat annotation, give it higher priority.
JsonFormat.Value precedenceFormat = super.findFormat(ann);
if (precedenceFormat != null) {
return precedenceFormat;
}

return ARRAY_FORMAT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
//
package org.msgpack.jackson.dataformat;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.containsString;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class MessagePackDataformatForPojoTest
Expand Down Expand Up @@ -99,4 +105,18 @@ public void testChangingPropertyNames()
ChangingPropertyNamesPojo value = objectMapper.readValue(bytes, ChangingPropertyNamesPojo.class);
assertEquals("komamitsu", value.getTheName());
}

@Test
public void testSerializationWithoutSchema()
throws IOException
{
ObjectMapper objectMapper = new ObjectMapper(factory); // to not affect shared objectMapper state
UsingCustomConstructorPojo orig = new UsingCustomConstructorPojo("komamitsu", 55);
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
byte[] bytes = objectMapper.writeValueAsBytes(orig);
String scheme = new String(bytes, Charset.forName("UTF-8"));
assertThat(scheme, not(containsString("name")));
UsingCustomConstructorPojo value = objectMapper.readValue(bytes, UsingCustomConstructorPojo.class);
assertEquals("komamitsu", value.name);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add a test for '55' (age) as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, added

}
}