| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,12 @@ public ExtensionTypeCustomDeserializers() | |||
| 32 | 32 | objectMapper = new ObjectMapper(new MessagePackFactory().setReuseResourceInParser(false)); | |
| 33 | 33 | } | |
| 34 | 34 | ||
| 35 | + public ExtensionTypeCustomDeserializers(ExtensionTypeCustomDeserializers src) | ||
| 36 | + { | ||
| 37 | + this(); | ||
| 38 | + this.deserTable.putAll(src.deserTable); | ||
| 39 | + } | ||
| 40 | + | ||
| 35 | 41 | public <T> void addTargetClass(byte type, final Class<T> klass) | |
| 36 | 42 | { | |
| 37 | 43 | deserTable.put(type, new Deser() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | import com.fasterxml.jackson.core.JsonParser; | |
| 23 | 23 | import com.fasterxml.jackson.core.io.IOContext; | |
| 24 | 24 | import org.msgpack.core.MessagePack; | |
| 25 | + import org.msgpack.core.annotations.VisibleForTesting; | ||
| 25 | 26 | ||
| 26 | 27 | import java.io.File; | |
| 27 | 28 | import java.io.FileOutputStream; | |
@@ -51,6 +52,15 @@ public MessagePackFactory(MessagePack.PackerConfig packerConfig) | |||
| 51 | 52 | this.packerConfig = packerConfig; | |
| 52 | 53 | } | |
| 53 | 54 | ||
| 55 | + public MessagePackFactory(MessagePackFactory src) | ||
| 56 | + { | ||
| 57 | + super(src, null); | ||
| 58 | + this.packerConfig = src.packerConfig.clone(); | ||
| 59 | + this.reuseResourceInGenerator = src.reuseResourceInGenerator; | ||
| 60 | + this.reuseResourceInParser = src.reuseResourceInParser; | ||
| 61 | + this.extTypeCustomDesers = new ExtensionTypeCustomDeserializers(src.extTypeCustomDesers); | ||
| 62 | + } | ||
| 63 | + | ||
| 54 | 64 | public MessagePackFactory setReuseResourceInGenerator(boolean reuseResourceInGenerator) | |
| 55 | 65 | { | |
| 56 | 66 | this.reuseResourceInGenerator = reuseResourceInGenerator; | |
@@ -130,4 +140,34 @@ protected JsonParser _createParser(byte[] data, int offset, int len, IOContext c | |||
| 130 | 140 | } | |
| 131 | 141 | return parser; | |
| 132 | 142 | } | |
| 143 | + | ||
| 144 | + @Override | ||
| 145 | + public JsonFactory copy() | ||
| 146 | + { | ||
| 147 | + return new MessagePackFactory(this); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + @VisibleForTesting | ||
| 151 | + MessagePack.PackerConfig getPackerConfig() | ||
| 152 | + { | ||
| 153 | + return packerConfig; | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + @VisibleForTesting | ||
| 157 | + boolean isReuseResourceInGenerator() | ||
| 158 | + { | ||
| 159 | + return reuseResourceInGenerator; | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + @VisibleForTesting | ||
| 163 | + boolean isReuseResourceInParser() | ||
| 164 | + { | ||
| 165 | + return reuseResourceInParser; | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + @VisibleForTesting | ||
| 169 | + ExtensionTypeCustomDeserializers getExtTypeCustomDesers() | ||
| 170 | + { | ||
| 171 | + return extTypeCustomDesers; | ||
| 172 | + } | ||
| 133 | 173 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,13 +16,26 @@ | |||
| 16 | 16 | package org.msgpack.jackson.dataformat; | |
| 17 | 17 | ||
| 18 | 18 | import com.fasterxml.jackson.core.JsonEncoding; | |
| 19 | + import com.fasterxml.jackson.core.JsonFactory; | ||
| 19 | 20 | import com.fasterxml.jackson.core.JsonGenerator; | |
| 20 | 21 | import com.fasterxml.jackson.core.JsonParser; | |
| 22 | + import com.fasterxml.jackson.core.type.TypeReference; | ||
| 23 | + import com.fasterxml.jackson.databind.AnnotationIntrospector; | ||
| 24 | + import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 21 | 25 | import org.junit.Test; | |
| 26 | + import org.msgpack.core.MessagePack; | ||
| 22 | 27 | ||
| 23 | 28 | import java.io.IOException; | |
| 29 | + import java.util.Collection; | ||
| 30 | + import java.util.HashMap; | ||
| 31 | + import java.util.Map; | ||
| 24 | 32 | ||
| 33 | + import static org.hamcrest.CoreMatchers.instanceOf; | ||
| 34 | + import static org.hamcrest.CoreMatchers.is; | ||
| 35 | + import static org.hamcrest.CoreMatchers.notNullValue; | ||
| 36 | + import static org.hamcrest.CoreMatchers.nullValue; | ||
| 25 | 37 | import static org.junit.Assert.assertEquals; | |
| 38 | + import static org.junit.Assert.assertThat; | ||
| 26 | 39 | ||
| 27 | 40 | public class MessagePackFactoryTest | |
| 28 | 41 | extends MessagePackDataformatTestBase | |
@@ -43,4 +56,48 @@ public void testCreateParser() | |||
| 43 | 56 | JsonParser parser = factory.createParser(in); | |
| 44 | 57 | assertEquals(MessagePackParser.class, parser.getClass()); | |
| 45 | 58 | } | |
| 59 | + | ||
| 60 | + @Test | ||
| 61 | + public void copy() | ||
| 62 | + throws IOException | ||
| 63 | + { | ||
| 64 | + ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers(); | ||
| 65 | + extTypeCustomDesers.addTargetClass((byte) 42, TinyPojo.class); | ||
| 66 | + | ||
| 67 | + MessagePack.PackerConfig msgpackPackerConfig = new MessagePack.PackerConfig().withStr8FormatSupport(false); | ||
| 68 | + | ||
| 69 | + MessagePackFactory messagePackFactory = new MessagePackFactory(msgpackPackerConfig); | ||
| 70 | + messagePackFactory.setExtTypeCustomDesers(extTypeCustomDesers); | ||
| 71 | + | ||
| 72 | + ObjectMapper objectMapper = new ObjectMapper(messagePackFactory); | ||
| 73 | + | ||
| 74 | + objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); | ||
| 75 | + objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); | ||
| 76 | + | ||
| 77 | + objectMapper.setAnnotationIntrospector(new JsonArrayFormat()); | ||
| 78 | + | ||
| 79 | + ObjectMapper copiedObjectMapper = objectMapper.copy(); | ||
| 80 | + JsonFactory copiedFactory = copiedObjectMapper.getFactory(); | ||
| 81 | + assertThat(copiedFactory, is(instanceOf(MessagePackFactory.class))); | ||
| 82 | + MessagePackFactory copiedMessagePackFactory = (MessagePackFactory) copiedFactory; | ||
| 83 | + | ||
| 84 | + assertThat(copiedMessagePackFactory.getPackerConfig().isStr8FormatSupport(), is(false)); | ||
| 85 | + | ||
| 86 | + assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 42), is(notNullValue())); | ||
| 87 | + assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 43), is(nullValue())); | ||
| 88 | + | ||
| 89 | + assertThat(copiedMessagePackFactory.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET), is(false)); | ||
| 90 | + assertThat(copiedMessagePackFactory.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE), is(false)); | ||
| 91 | + | ||
| 92 | + Collection<AnnotationIntrospector> annotationIntrospectors = copiedObjectMapper.getSerializationConfig().getAnnotationIntrospector().allIntrospectors(); | ||
| 93 | + assertThat(annotationIntrospectors.size(), is(1)); | ||
| 94 | + assertThat(annotationIntrospectors.stream().findFirst().get(), is(instanceOf(JsonArrayFormat.class))); | ||
| 95 | + | ||
| 96 | + HashMap<String, Integer> map = new HashMap<>(); | ||
| 97 | + map.put("one", 1); | ||
| 98 | + Map<String, Integer> deserialized = copiedObjectMapper | ||
| 99 | + .readValue(objectMapper.writeValueAsBytes(map), new TypeReference<Map<String, Integer>>() {}); | ||
| 100 | + assertThat(deserialized.size(), is(1)); | ||
| 101 | + assertThat(deserialized.get("one"), is(1)); | ||
| 102 | + } | ||
| 46 | 103 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments