|
15 | 15 | // |
16 | 16 | package org.msgpack.jackson.dataformat; |
17 | 17 |
|
| 18 | +import com.fasterxml.jackson.core.JsonGenerator; |
18 | 19 | import com.fasterxml.jackson.core.JsonParser; |
19 | 20 | import com.fasterxml.jackson.core.JsonProcessingException; |
20 | 21 | import com.fasterxml.jackson.core.JsonToken; |
21 | 22 | import com.fasterxml.jackson.core.io.JsonEOFException; |
22 | 23 | import com.fasterxml.jackson.core.type.TypeReference; |
23 | 24 | import com.fasterxml.jackson.databind.DeserializationContext; |
24 | 25 | import com.fasterxml.jackson.databind.DeserializationFeature; |
| 26 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
25 | 27 | import com.fasterxml.jackson.databind.JsonMappingException; |
26 | 28 | import com.fasterxml.jackson.databind.KeyDeserializer; |
27 | 29 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | +import com.fasterxml.jackson.databind.SerializerProvider; |
28 | 31 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 32 | +import com.fasterxml.jackson.databind.ser.std.StdSerializer; |
29 | 33 | import org.junit.Test; |
30 | 34 | import org.msgpack.core.MessagePack; |
31 | 35 | import org.msgpack.core.MessagePacker; |
| 36 | +import org.msgpack.value.ExtensionValue; |
| 37 | +import org.msgpack.value.MapValue; |
| 38 | +import org.msgpack.value.ValueFactory; |
32 | 39 |
|
33 | 40 | import java.io.ByteArrayInputStream; |
34 | 41 | import java.io.ByteArrayOutputStream; |
|
44 | 51 | import java.util.HashMap; |
45 | 52 | import java.util.List; |
46 | 53 | import java.util.Map; |
| 54 | +import java.util.UUID; |
47 | 55 |
|
48 | 56 | import static org.hamcrest.MatcherAssert.assertThat; |
49 | 57 | import static org.hamcrest.core.Is.is; |
@@ -775,6 +783,105 @@ public Object deserialize(byte[] data) |
775 | 783 | assertThat((String) values.get(4), is("Java")); |
776 | 784 | } |
777 | 785 |
|
| 786 | + |
| 787 | + static class UUIDSerializer |
| 788 | + extends StdSerializer<UUID> |
| 789 | + { |
| 790 | + private final byte code; |
| 791 | + |
| 792 | + UUIDSerializer(byte code) |
| 793 | + { |
| 794 | + super(UUID.class); |
| 795 | + this.code = code; |
| 796 | + } |
| 797 | + |
| 798 | + public void serialize(UUID value, JsonGenerator jsonGenerator, SerializerProvider provider) |
| 799 | + throws IOException |
| 800 | + { |
| 801 | + if (jsonGenerator instanceof MessagePackGenerator) { |
| 802 | + MessagePackGenerator messagePackGenerator = (MessagePackGenerator) jsonGenerator; |
| 803 | + messagePackGenerator.writeExtensionType(new MessagePackExtensionType(code, toBytes(value))); |
| 804 | + } else { |
| 805 | + throw new RuntimeException("Something went wrong with the serialization"); |
| 806 | + } |
| 807 | + } |
| 808 | + |
| 809 | + @SuppressWarnings("WeakerAccess") |
| 810 | + static byte[] toBytes(UUID value) |
| 811 | + { |
| 812 | + return value.toString().getBytes(); |
| 813 | + } |
| 814 | + |
| 815 | + static UUID fromBytes(byte[] value) |
| 816 | + { |
| 817 | + return UUID.fromString(new String(value)); |
| 818 | + } |
| 819 | + } |
| 820 | + |
| 821 | + @Test |
| 822 | + public void extensionTypeInMap() |
| 823 | + throws IOException |
| 824 | + { |
| 825 | + byte uuidTypeCode = 42; |
| 826 | + |
| 827 | + ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers(); |
| 828 | + extTypeCustomDesers.addCustomDeser(uuidTypeCode, new ExtensionTypeCustomDeserializers.Deser() |
| 829 | + { |
| 830 | + @Override |
| 831 | + public Object deserialize(byte[] value1) |
| 832 | + throws IOException |
| 833 | + { |
| 834 | + return UUIDSerializer.fromBytes(value1); |
| 835 | + } |
| 836 | + }); |
| 837 | + |
| 838 | + ObjectMapper objectMapper = new ObjectMapper( |
| 839 | + new MessagePackFactory().setExtTypeCustomDesers(extTypeCustomDesers)); |
| 840 | + |
| 841 | + SimpleModule simpleModule = new SimpleModule(); |
| 842 | + simpleModule.addDeserializer(UUID.class, |
| 843 | + new JsonDeserializer<UUID>() |
| 844 | + { |
| 845 | + @Override |
| 846 | + public UUID deserialize(JsonParser p, DeserializationContext ctxt) |
| 847 | + throws IOException, JsonProcessingException |
| 848 | + { |
| 849 | + return UUID.fromString(p.readValueAs(String.class)); |
| 850 | + } |
| 851 | + }); |
| 852 | + objectMapper.registerModule(simpleModule); |
| 853 | + |
| 854 | + // Prepare serialized data |
| 855 | + Map<UUID, UUID> originalMap = new HashMap<>(); |
| 856 | + byte[] serializedData; |
| 857 | + { |
| 858 | + ValueFactory.MapBuilder mapBuilder = ValueFactory.newMapBuilder(); |
| 859 | + for (int i = 0; i < 4; i++) { |
| 860 | + UUID uuidKey = UUID.randomUUID(); |
| 861 | + UUID uuidValue = UUID.randomUUID(); |
| 862 | + ExtensionValue k = ValueFactory.newExtension(uuidTypeCode, uuidKey.toString().getBytes()); |
| 863 | + ExtensionValue v = ValueFactory.newExtension(uuidTypeCode, uuidValue.toString().getBytes()); |
| 864 | + mapBuilder.put(k, v); |
| 865 | + originalMap.put(uuidKey, uuidValue); |
| 866 | + } |
| 867 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 868 | + MessagePacker packer = MessagePack.newDefaultPacker(output); |
| 869 | + MapValue mapValue = mapBuilder.build(); |
| 870 | + mapValue.writeTo(packer); |
| 871 | + packer.close(); |
| 872 | + |
| 873 | + serializedData = output.toByteArray(); |
| 874 | + } |
| 875 | + |
| 876 | + Map<UUID, UUID> deserializedMap = objectMapper.readValue(serializedData, |
| 877 | + new TypeReference<Map<UUID, UUID>>() {}); |
| 878 | + |
| 879 | + assertEquals(originalMap.size(), deserializedMap.size()); |
| 880 | + for (Map.Entry<UUID, UUID> entry : originalMap.entrySet()) { |
| 881 | + assertEquals(entry.getValue(), deserializedMap.get(entry.getKey())); |
| 882 | + } |
| 883 | + } |
| 884 | + |
778 | 885 | @Test |
779 | 886 | public void parserShouldReadStrAsBin() |
780 | 887 | throws IOException |
|
0 commit comments