You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While reading an object with a generic type map, Kryo will create a MapSerializer and set the generic type on the serializer. When the FieldSerializer encounters a reference to the same map, it will again set the generic types of the MapSerializer, but will return without re-setting the generic types.
After this, a subsequent request to serialize a map with different generic types different will lead to a ClassCastException, as demonstrated in the test case below:
@Test
public void testSerializeMapAfterDeserialization() throws IOException {
Kryo kryo = new Kryo();
byte[] serializedMapHolder = serialize(kryo, new MapHolder());
deserialize(kryo, serializedMapHolder);
Map<Integer, List<String>> stringMap = new HashMap<>();
stringMap.put(1, new ArrayList<>());
serialize(kryo, stringMap);
}
static class MapHolder {
private Map<Integer, String> mapOne = new HashMap<>();
private Map<Integer, String> mapTwo = this.mapOne;
}
private byte[] serialize(Kryo kryo, Object object) throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Output output = new Output(outputStream)) {
kryo.writeClassAndObject(output, object);
output.flush();
return outputStream.toByteArray();
}
}
private Object deserialize(Kryo kryo, byte[] serializedBytes) throws IOException {
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(serializedBytes); Input input = new Input(inputStream)) {
return kryo.readClassAndObject(input);
}
}
This test case results in the following error:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
at com.esotericsoftware.kryo.serializers.DefaultSerializers$StringSerializer.write(DefaultSerializers.java:184)
at com.esotericsoftware.kryo.Kryo.writeObjectOrNull(Kryo.java:603)
at com.esotericsoftware.kryo.serializers.MapSerializer.write(MapSerializer.java:109)
at com.esotericsoftware.kryo.serializers.MapSerializer.write(MapSerializer.java:39)
at com.esotericsoftware.kryo.Kryo.writeClassAndObject(Kryo.java:625)
at com.test.MapSerializerTest.serialize(MapSerializerTest.java:36)
at com.test.MapSerializerTest.testSerializeMapAfterDeserialization(MapSerializerTest.java:26)
The text was updated successfully, but these errors were encountered:
While reading an object with a generic type map, Kryo will create a MapSerializer and set the generic type on the serializer. When the FieldSerializer encounters a reference to the same map, it will again set the generic types of the MapSerializer, but will return without re-setting the generic types.
After this, a subsequent request to serialize a map with different generic types different will lead to a ClassCastException, as demonstrated in the test case below:
This test case results in the following error:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
The text was updated successfully, but these errors were encountered: