Skip to content

Commit a4b0f3f

Browse files
committed
Override MessagePackExtensionType#equals and hashCode
Also, added an unit test of deserialization of MessagePackExtensionType based on type.
1 parent 78d8bb3 commit a4b0f3f

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackExtensionType.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
88

99
import java.io.IOException;
10+
import java.util.Arrays;
1011

1112
@JsonSerialize(using = MessagePackExtensionType.Serializer.class)
1213
public class MessagePackExtensionType
@@ -30,6 +31,32 @@ public byte[] getData()
3031
return data;
3132
}
3233

34+
@Override
35+
public boolean equals(Object o)
36+
{
37+
if (this == o) {
38+
return true;
39+
}
40+
if (!(o instanceof MessagePackExtensionType)) {
41+
return false;
42+
}
43+
44+
MessagePackExtensionType that = (MessagePackExtensionType) o;
45+
46+
if (type != that.type) {
47+
return false;
48+
}
49+
return Arrays.equals(data, that.data);
50+
}
51+
52+
@Override
53+
public int hashCode()
54+
{
55+
int result = (int) type;
56+
result = 31 * result + Arrays.hashCode(data);
57+
return result;
58+
}
59+
3360
public static class Serializer extends JsonSerializer<MessagePackExtensionType>
3461
{
3562
@Override

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackParserTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
import com.fasterxml.jackson.databind.DeserializationFeature;
2424
import com.fasterxml.jackson.databind.KeyDeserializer;
2525
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer;
2627
import com.fasterxml.jackson.databind.module.SimpleModule;
2728
import org.junit.Test;
2829
import org.msgpack.core.MessagePack;
2930
import org.msgpack.core.MessagePacker;
3031

32+
import java.io.ByteArrayInputStream;
3133
import java.io.ByteArrayOutputStream;
3234
import java.io.File;
3335
import java.io.FileInputStream;
@@ -41,6 +43,9 @@
4143
import java.util.List;
4244
import java.util.Map;
4345

46+
import static org.hamcrest.MatcherAssert.assertThat;
47+
import static org.hamcrest.core.Is.is;
48+
import static org.hamcrest.core.IsInstanceOf.instanceOf;
4449
import static org.junit.Assert.assertArrayEquals;
4550
import static org.junit.Assert.assertEquals;
4651
import static org.junit.Assert.assertNull;
@@ -671,4 +676,55 @@ public Object deserializeKey(String key, DeserializationContext ctxt)
671676
assertEquals((Integer) 2, map.get(true));
672677
assertEquals((Integer) 3, map.get(false));
673678
}
679+
680+
public static class MyExtTypeDeserializer extends UntypedObjectDeserializer.Vanilla
681+
{
682+
@Override
683+
public Object deserialize(JsonParser p, DeserializationContext ctxt)
684+
throws IOException
685+
{
686+
Object obj = super.deserialize(p, ctxt);
687+
if (obj instanceof MessagePackExtensionType) {
688+
MessagePackExtensionType ext = (MessagePackExtensionType) obj;
689+
if (ext.getType() == 31) {
690+
if (Arrays.equals(ext.getData(), new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE})) {
691+
return "Java";
692+
}
693+
return "Not Java";
694+
}
695+
}
696+
return obj;
697+
}
698+
}
699+
700+
@Test
701+
public void customDeserializationForExtType()
702+
throws IOException
703+
{
704+
ByteArrayOutputStream out = new ByteArrayOutputStream();
705+
MessagePacker packer = MessagePack.newDefaultPacker(out);
706+
packer.packArrayHeader(4);
707+
packer.packString("foo bar");
708+
packer.packExtensionTypeHeader((byte) 31, 4);
709+
packer.addPayload(new byte[] {(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE});
710+
packer.packArrayHeader(1);
711+
packer.packInt(42);
712+
packer.packExtensionTypeHeader((byte) 32, 2);
713+
packer.addPayload(new byte[] {(byte) 0xAB, (byte) 0xCD});
714+
packer.close();
715+
716+
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
717+
SimpleModule module = new SimpleModule("MyModule").addDeserializer(Object.class, new MyExtTypeDeserializer());
718+
objectMapper.registerModule(module);
719+
720+
List<Object> values = objectMapper.readValue(new ByteArrayInputStream(out.toByteArray()), new TypeReference<List<Object>>() {});
721+
assertThat(values.size(), is(4));
722+
assertThat((String) values.get(0), is("foo bar"));
723+
assertThat((String) values.get(1), is("Java"));
724+
assertThat(values.get(2), is(instanceOf(List.class)));
725+
List<Object> nested = (List<Object>) values.get(2);
726+
assertThat(nested.size(), is(1));
727+
assertThat((Integer) nested.get(0), is(42));
728+
assertThat((MessagePackExtensionType) values.get(3), is(new MessagePackExtensionType((byte) 32, new byte[] {(byte) 0xAB, (byte) 0xCD})));
729+
}
674730
}

0 commit comments

Comments
 (0)