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
Prev Previous commit
Next Next commit
Add MessagePackParserTest#extensionTypeWithPojoInMap
  • Loading branch information
komamitsu committed Dec 13, 2020
commit 4b2e4a1b1db9d04c490fc017c0c14fc097b35fb3
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ public byte[] getBinaryValue(Base64Variant b64variant)
return bytesValue;
case STRING:
return stringValue.getBytes(MessagePack.UTF8);
case EXT:
return extensionTypeValue.getData();
default:
throw new IllegalStateException("Invalid type=" + type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.Test;
import org.msgpack.core.MessagePack;
Expand Down Expand Up @@ -779,14 +780,160 @@ public Object deserialize(byte[] data)
assertThat((String) values.get(4), is("Java"));
}

static class TripleBytesPojo
{
public byte first;
public byte second;
public byte third;

public TripleBytesPojo(byte first, byte second, byte third)
{
this.first = first;
this.second = second;
this.third = third;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (!(o instanceof TripleBytesPojo)) {
return false;
}

TripleBytesPojo that = (TripleBytesPojo) o;

if (first != that.first) {
return false;
}
if (second != that.second) {
return false;
}
return third == that.third;
}

@Override
public int hashCode()
{
int result = first;
result = 31 * result + (int) second;
result = 31 * result + (int) third;
return result;
}

@Override
public String toString()
{
return String.format("%d-%d-%d", first, second, third);
}

static class Deserializer
extends StdDeserializer<TripleBytesPojo>
{
protected Deserializer()
{
super(TripleBytesPojo.class);
}

@Override
public TripleBytesPojo deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
return TripleBytesPojo.deserialize(p.getBinaryValue());
}
}

static class KeyDeserializer
extends com.fasterxml.jackson.databind.KeyDeserializer
{
@Override
public Object deserializeKey(String key, DeserializationContext ctxt)
throws IOException
{
String[] values = key.split("-");
return new TripleBytesPojo(
Byte.parseByte(values[0]),
Byte.parseByte(values[1]),
Byte.parseByte(values[2]));
}
}

static byte[] serialize(TripleBytesPojo obj)
{
return new byte[] { obj.first, obj.second, obj.third };
}

static TripleBytesPojo deserialize(byte[] bytes)
{
return new TripleBytesPojo(bytes[0], bytes[1], bytes[2]);
}
}

@Test
public void extensionTypeWithPojoInMap()
throws IOException
{
byte extTypeCode = 42;

ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers();
extTypeCustomDesers.addCustomDeser(extTypeCode, new ExtensionTypeCustomDeserializers.Deser()
{
@Override
public Object deserialize(byte[] value)
throws IOException
{
return TripleBytesPojo.deserialize(value);
}
});

SimpleModule module = new SimpleModule();
module.addDeserializer(TripleBytesPojo.class, new TripleBytesPojo.Deserializer());
module.addKeyDeserializer(TripleBytesPojo.class, new TripleBytesPojo.KeyDeserializer());
ObjectMapper objectMapper = new ObjectMapper(
new MessagePackFactory().setExtTypeCustomDesers(extTypeCustomDesers))
.registerModule(module);

// Prepare serialized data
Map<TripleBytesPojo, TripleBytesPojo> originalMap = new HashMap<>();
byte[] serializedData;
{
ValueFactory.MapBuilder mapBuilder = ValueFactory.newMapBuilder();
for (int i = 0; i < 4; i++) {
TripleBytesPojo keyObj = new TripleBytesPojo((byte) i, (byte) (i + 1), (byte) (i + 2));
TripleBytesPojo valueObj = new TripleBytesPojo((byte) (i * 2), (byte) (i * 3), (byte) (i * 4));
ExtensionValue k = ValueFactory.newExtension(extTypeCode, TripleBytesPojo.serialize(keyObj));
ExtensionValue v = ValueFactory.newExtension(extTypeCode, TripleBytesPojo.serialize(valueObj));
mapBuilder.put(k, v);
originalMap.put(keyObj, valueObj);
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
MessagePacker packer = MessagePack.newDefaultPacker(output);
MapValue mapValue = mapBuilder.build();
mapValue.writeTo(packer);
packer.close();

serializedData = output.toByteArray();
}

Map<TripleBytesPojo, TripleBytesPojo> deserializedMap = objectMapper.readValue(serializedData,
new TypeReference<Map<TripleBytesPojo, TripleBytesPojo>>() {});

assertEquals(originalMap.size(), deserializedMap.size());
for (Map.Entry<TripleBytesPojo, TripleBytesPojo> entry : originalMap.entrySet()) {
assertEquals(entry.getValue(), deserializedMap.get(entry.getKey()));
}
}

@Test
public void extensionTypeInMap()
public void extensionTypeWithUuidInMap()
throws IOException
{
byte uuidTypeCode = 42;
byte extTypeCode = 42;

ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers();
extTypeCustomDesers.addCustomDeser(uuidTypeCode, new ExtensionTypeCustomDeserializers.Deser()
extTypeCustomDesers.addCustomDeser(extTypeCode, new ExtensionTypeCustomDeserializers.Deser()
{
@Override
public Object deserialize(byte[] value)
Expand All @@ -805,12 +952,12 @@ public Object deserialize(byte[] value)
{
ValueFactory.MapBuilder mapBuilder = ValueFactory.newMapBuilder();
for (int i = 0; i < 4; i++) {
UUID uuidKey = UUID.randomUUID();
UUID uuidValue = UUID.randomUUID();
ExtensionValue k = ValueFactory.newExtension(uuidTypeCode, uuidKey.toString().getBytes());
ExtensionValue v = ValueFactory.newExtension(uuidTypeCode, uuidValue.toString().getBytes());
UUID keyObj = UUID.randomUUID();
UUID valueObj = UUID.randomUUID();
ExtensionValue k = ValueFactory.newExtension(extTypeCode, keyObj.toString().getBytes());
ExtensionValue v = ValueFactory.newExtension(extTypeCode, valueObj.toString().getBytes());
mapBuilder.put(k, v);
originalMap.put(uuidKey, uuidValue);
originalMap.put(keyObj, valueObj);
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
MessagePacker packer = MessagePack.newDefaultPacker(output);
Expand Down