diff --git a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/ExtensionTypeCustomDeserializers.java b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/ExtensionTypeCustomDeserializers.java index 6d61ce53b..ef86ebbb8 100644 --- a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/ExtensionTypeCustomDeserializers.java +++ b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/ExtensionTypeCustomDeserializers.java @@ -32,6 +32,12 @@ public ExtensionTypeCustomDeserializers() objectMapper = new ObjectMapper(new MessagePackFactory().setReuseResourceInParser(false)); } + public ExtensionTypeCustomDeserializers(ExtensionTypeCustomDeserializers src) + { + this(); + this.deserTable.putAll(src.deserTable); + } + public void addTargetClass(byte type, final Class klass) { deserTable.put(type, new Deser() diff --git a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java index 11b5ee88f..a624b6b9d 100644 --- a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java +++ b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.io.IOContext; import org.msgpack.core.MessagePack; +import org.msgpack.core.annotations.VisibleForTesting; import java.io.File; import java.io.FileOutputStream; @@ -51,6 +52,15 @@ public MessagePackFactory(MessagePack.PackerConfig packerConfig) this.packerConfig = packerConfig; } + public MessagePackFactory(MessagePackFactory src) + { + super(src, null); + this.packerConfig = src.packerConfig.clone(); + this.reuseResourceInGenerator = src.reuseResourceInGenerator; + this.reuseResourceInParser = src.reuseResourceInParser; + this.extTypeCustomDesers = new ExtensionTypeCustomDeserializers(src.extTypeCustomDesers); + } + public MessagePackFactory setReuseResourceInGenerator(boolean reuseResourceInGenerator) { this.reuseResourceInGenerator = reuseResourceInGenerator; @@ -130,4 +140,34 @@ protected JsonParser _createParser(byte[] data, int offset, int len, IOContext c } return parser; } + + @Override + public JsonFactory copy() + { + return new MessagePackFactory(this); + } + + @VisibleForTesting + MessagePack.PackerConfig getPackerConfig() + { + return packerConfig; + } + + @VisibleForTesting + boolean isReuseResourceInGenerator() + { + return reuseResourceInGenerator; + } + + @VisibleForTesting + boolean isReuseResourceInParser() + { + return reuseResourceInParser; + } + + @VisibleForTesting + ExtensionTypeCustomDeserializers getExtTypeCustomDesers() + { + return extTypeCustomDesers; + } } diff --git a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java index 25180f784..f9ce8629c 100644 --- a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java +++ b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java @@ -16,13 +16,26 @@ package org.msgpack.jackson.dataformat; import com.fasterxml.jackson.core.JsonEncoding; +import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.AnnotationIntrospector; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; +import org.msgpack.core.MessagePack; import java.io.IOException; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; public class MessagePackFactoryTest extends MessagePackDataformatTestBase @@ -43,4 +56,48 @@ public void testCreateParser() JsonParser parser = factory.createParser(in); assertEquals(MessagePackParser.class, parser.getClass()); } + + @Test + public void copy() + throws IOException + { + ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers(); + extTypeCustomDesers.addTargetClass((byte) 42, TinyPojo.class); + + MessagePack.PackerConfig msgpackPackerConfig = new MessagePack.PackerConfig().withStr8FormatSupport(false); + + MessagePackFactory messagePackFactory = new MessagePackFactory(msgpackPackerConfig); + messagePackFactory.setExtTypeCustomDesers(extTypeCustomDesers); + + ObjectMapper objectMapper = new ObjectMapper(messagePackFactory); + + objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); + objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); + + objectMapper.setAnnotationIntrospector(new JsonArrayFormat()); + + ObjectMapper copiedObjectMapper = objectMapper.copy(); + JsonFactory copiedFactory = copiedObjectMapper.getFactory(); + assertThat(copiedFactory, is(instanceOf(MessagePackFactory.class))); + MessagePackFactory copiedMessagePackFactory = (MessagePackFactory) copiedFactory; + + assertThat(copiedMessagePackFactory.getPackerConfig().isStr8FormatSupport(), is(false)); + + assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 42), is(notNullValue())); + assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 43), is(nullValue())); + + assertThat(copiedMessagePackFactory.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET), is(false)); + assertThat(copiedMessagePackFactory.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE), is(false)); + + Collection annotationIntrospectors = copiedObjectMapper.getSerializationConfig().getAnnotationIntrospector().allIntrospectors(); + assertThat(annotationIntrospectors.size(), is(1)); + assertThat(annotationIntrospectors.stream().findFirst().get(), is(instanceOf(JsonArrayFormat.class))); + + HashMap map = new HashMap<>(); + map.put("one", 1); + Map deserialized = copiedObjectMapper + .readValue(objectMapper.writeValueAsBytes(map), new TypeReference>() {}); + assertThat(deserialized.size(), is(1)); + assertThat(deserialized.get("one"), is(1)); + } }