FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Support ObjectMapper#copy with MessagePackFactory by komamitsu · Pull Request #454 · msgpack/msgpack-java · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public ExtensionTypeCustomDeserializers()
objectMapper = new ObjectMapper(new MessagePackFactory().setReuseResourceInParser(false));
}

public ExtensionTypeCustomDeserializers(ExtensionTypeCustomDeserializers src)
{
this();
this.deserTable.putAll(src.deserTable);
}

public <T> void addTargetClass(byte type, final Class<T> klass)
{
deserTable.put(type, new Deser()
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<AnnotationIntrospector> annotationIntrospectors = copiedObjectMapper.getSerializationConfig().getAnnotationIntrospector().allIntrospectors();
assertThat(annotationIntrospectors.size(), is(1));
assertThat(annotationIntrospectors.stream().findFirst().get(), is(instanceOf(JsonArrayFormat.class)));

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
Map<String, Integer> deserialized = copiedObjectMapper
.readValue(objectMapper.writeValueAsBytes(map), new TypeReference<Map<String, Integer>>() {});
assertThat(deserialized.size(), is(1));
assertThat(deserialized.get("one"), is(1));
}
}

Back | FazBrowse Home | New Git URL