2525import com .fasterxml .jackson .databind .JsonMappingException ;
2626import com .fasterxml .jackson .databind .KeyDeserializer ;
2727import com .fasterxml .jackson .databind .ObjectMapper ;
28+ import com .fasterxml .jackson .databind .deser .std .StdDeserializer ;
2829import com .fasterxml .jackson .databind .module .SimpleModule ;
2930import org .junit .Test ;
3031import org .msgpack .core .MessagePack ;
@@ -779,14 +780,160 @@ public Object deserialize(byte[] data)
779780 assertThat ((String ) values .get (4 ), is ("Java" ));
780781 }
781782
783+ static class TripleBytesPojo
784+ {
785+ public byte first ;
786+ public byte second ;
787+ public byte third ;
788+
789+ public TripleBytesPojo (byte first , byte second , byte third )
790+ {
791+ this .first = first ;
792+ this .second = second ;
793+ this .third = third ;
794+ }
795+
796+ @ Override
797+ public boolean equals (Object o )
798+ {
799+ if (this == o ) {
800+ return true ;
801+ }
802+ if (!(o instanceof TripleBytesPojo )) {
803+ return false ;
804+ }
805+
806+ TripleBytesPojo that = (TripleBytesPojo ) o ;
807+
808+ if (first != that .first ) {
809+ return false ;
810+ }
811+ if (second != that .second ) {
812+ return false ;
813+ }
814+ return third == that .third ;
815+ }
816+
817+ @ Override
818+ public int hashCode ()
819+ {
820+ int result = first ;
821+ result = 31 * result + (int ) second ;
822+ result = 31 * result + (int ) third ;
823+ return result ;
824+ }
825+
826+ @ Override
827+ public String toString ()
828+ {
829+ return String .format ("%d-%d-%d" , first , second , third );
830+ }
831+
832+ static class Deserializer
833+ extends StdDeserializer <TripleBytesPojo >
834+ {
835+ protected Deserializer ()
836+ {
837+ super (TripleBytesPojo .class );
838+ }
839+
840+ @ Override
841+ public TripleBytesPojo deserialize (JsonParser p , DeserializationContext ctxt )
842+ throws IOException , JsonProcessingException
843+ {
844+ return TripleBytesPojo .deserialize (p .getBinaryValue ());
845+ }
846+ }
847+
848+ static class KeyDeserializer
849+ extends com .fasterxml .jackson .databind .KeyDeserializer
850+ {
851+ @ Override
852+ public Object deserializeKey (String key , DeserializationContext ctxt )
853+ throws IOException
854+ {
855+ String [] values = key .split ("-" );
856+ return new TripleBytesPojo (
857+ Byte .parseByte (values [0 ]),
858+ Byte .parseByte (values [1 ]),
859+ Byte .parseByte (values [2 ]));
860+ }
861+ }
862+
863+ static byte [] serialize (TripleBytesPojo obj )
864+ {
865+ return new byte [] { obj .first , obj .second , obj .third };
866+ }
867+
868+ static TripleBytesPojo deserialize (byte [] bytes )
869+ {
870+ return new TripleBytesPojo (bytes [0 ], bytes [1 ], bytes [2 ]);
871+ }
872+ }
873+
874+ @ Test
875+ public void extensionTypeWithPojoInMap ()
876+ throws IOException
877+ {
878+ byte extTypeCode = 42 ;
879+
880+ ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers ();
881+ extTypeCustomDesers .addCustomDeser (extTypeCode , new ExtensionTypeCustomDeserializers .Deser ()
882+ {
883+ @ Override
884+ public Object deserialize (byte [] value )
885+ throws IOException
886+ {
887+ return TripleBytesPojo .deserialize (value );
888+ }
889+ });
890+
891+ SimpleModule module = new SimpleModule ();
892+ module .addDeserializer (TripleBytesPojo .class , new TripleBytesPojo .Deserializer ());
893+ module .addKeyDeserializer (TripleBytesPojo .class , new TripleBytesPojo .KeyDeserializer ());
894+ ObjectMapper objectMapper = new ObjectMapper (
895+ new MessagePackFactory ().setExtTypeCustomDesers (extTypeCustomDesers ))
896+ .registerModule (module );
897+
898+ // Prepare serialized data
899+ Map <TripleBytesPojo , TripleBytesPojo > originalMap = new HashMap <>();
900+ byte [] serializedData ;
901+ {
902+ ValueFactory .MapBuilder mapBuilder = ValueFactory .newMapBuilder ();
903+ for (int i = 0 ; i < 4 ; i ++) {
904+ TripleBytesPojo keyObj = new TripleBytesPojo ((byte ) i , (byte ) (i + 1 ), (byte ) (i + 2 ));
905+ TripleBytesPojo valueObj = new TripleBytesPojo ((byte ) (i * 2 ), (byte ) (i * 3 ), (byte ) (i * 4 ));
906+ ExtensionValue k = ValueFactory .newExtension (extTypeCode , TripleBytesPojo .serialize (keyObj ));
907+ ExtensionValue v = ValueFactory .newExtension (extTypeCode , TripleBytesPojo .serialize (valueObj ));
908+ mapBuilder .put (k , v );
909+ originalMap .put (keyObj , valueObj );
910+ }
911+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
912+ MessagePacker packer = MessagePack .newDefaultPacker (output );
913+ MapValue mapValue = mapBuilder .build ();
914+ mapValue .writeTo (packer );
915+ packer .close ();
916+
917+ serializedData = output .toByteArray ();
918+ }
919+
920+ Map <TripleBytesPojo , TripleBytesPojo > deserializedMap = objectMapper .readValue (serializedData ,
921+ new TypeReference <Map <TripleBytesPojo , TripleBytesPojo >>() {});
922+
923+ assertEquals (originalMap .size (), deserializedMap .size ());
924+ for (Map .Entry <TripleBytesPojo , TripleBytesPojo > entry : originalMap .entrySet ()) {
925+ assertEquals (entry .getValue (), deserializedMap .get (entry .getKey ()));
926+ }
927+ }
928+
782929 @ Test
783- public void extensionTypeInMap ()
930+ public void extensionTypeWithUuidInMap ()
784931 throws IOException
785932 {
786- byte uuidTypeCode = 42 ;
933+ byte extTypeCode = 42 ;
787934
788935 ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers ();
789- extTypeCustomDesers .addCustomDeser (uuidTypeCode , new ExtensionTypeCustomDeserializers .Deser ()
936+ extTypeCustomDesers .addCustomDeser (extTypeCode , new ExtensionTypeCustomDeserializers .Deser ()
790937 {
791938 @ Override
792939 public Object deserialize (byte [] value )
@@ -805,12 +952,12 @@ public Object deserialize(byte[] value)
805952 {
806953 ValueFactory .MapBuilder mapBuilder = ValueFactory .newMapBuilder ();
807954 for (int i = 0 ; i < 4 ; i ++) {
808- UUID uuidKey = UUID .randomUUID ();
809- UUID uuidValue = UUID .randomUUID ();
810- ExtensionValue k = ValueFactory .newExtension (uuidTypeCode , uuidKey .toString ().getBytes ());
811- ExtensionValue v = ValueFactory .newExtension (uuidTypeCode , uuidValue .toString ().getBytes ());
955+ UUID keyObj = UUID .randomUUID ();
956+ UUID valueObj = UUID .randomUUID ();
957+ ExtensionValue k = ValueFactory .newExtension (extTypeCode , keyObj .toString ().getBytes ());
958+ ExtensionValue v = ValueFactory .newExtension (extTypeCode , valueObj .toString ().getBytes ());
812959 mapBuilder .put (k , v );
813- originalMap .put (uuidKey , uuidValue );
960+ originalMap .put (keyObj , valueObj );
814961 }
815962 ByteArrayOutputStream output = new ByteArrayOutputStream ();
816963 MessagePacker packer = MessagePack .newDefaultPacker (output );
0 commit comments