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
Hi,
I wrote a Serializable class Parent which holds 2 SerializablePair with different generics.
Kryo behaves very strangely and fails to deserialize my class because it assumes that the second SerializablePair is the same generics type as the first one (which is wrong).
Am i doing something wrong or is this a major bug in Kryo?
Full example code:
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.util.Date;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;
public class KryoMain {
public static void main(String[] args) {
//create original Parent object:
Parent origParent = new Parent();
origParent.setDateStringPair(new SerializablePair<Date, String>(new Date(), null));
//commenting out this line makes the test work but it means i can't use same class with different generics?!
origParent.setDoubleDoublePair(new SerializablePair<Double, Double>(1.1, 2.2));
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
//write:
Output output = new Output(byteOut);
getKryo().writeClassAndObject(output, origParent);
output.close();
//read:
Parent newParent = null;
Input input = new Input(byteOut.toByteArray());
//Kryo Exception is thrown in this line?!
newParent = (Parent) getKryo().readClassAndObject(input);
}
public static Kryo getKryo() {
final Kryo kryo = new Kryo();
kryo.setAsmEnabled(true);
kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
return kryo;
}
public static class SerializablePair<F extends Serializable, S extends Serializable> implements Serializable {
public F first;
public S second;
public SerializablePair() {
super();
}
public SerializablePair(final F first, final S second) {
this.first = first;
this.second = second;
}
@Override
public String toString() {
return "Pair [first=" + first + ", second=" + second + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((first == null) ? 0 : first.hashCode());
result = prime * result + ((second == null) ? 0 : second.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof SerializablePair))
return false;
SerializablePair other = (SerializablePair) obj;
if (first == null) {
if (other.first != null)
return false;
} else if (!first.equals(other.first))
return false;
if (second == null) {
if (other.second != null)
return false;
} else if (!second.equals(other.second))
return false;
return true;
}
}
public static class Parent implements Serializable {
private static final long serialVersionUID = 1L;
SerializablePair<Double, Double> doubleDoublePair;
SerializablePair<Date, String> dateStringPair;
public Parent() {
super();
}
public SerializablePair<Double, Double> getDoubleDoublePair() {
return doubleDoublePair;
}
public SerializablePair<Date, String> getDateStringPair() {
return dateStringPair;
}
public void setDoubleDoublePair(SerializablePair<Double, Double> doubleDoublePair) {
this.doubleDoublePair = doubleDoublePair;
}
public void setDateStringPair(SerializablePair<Date, String> dateStringPair) {
this.dateStringPair = dateStringPair;
}
@Override
public String toString() {
return "Parent [doubleDoublePair=" + doubleDoublePair + ", dateStringPair=" + dateStringPair + "]";
}
}
}
The text was updated successfully, but these errors were encountered:
Hi,
I wrote a Serializable class Parent which holds 2 SerializablePair with different generics.
Kryo behaves very strangely and fails to deserialize my class because it assumes that the second SerializablePair is the same generics type as the first one (which is wrong).
Am i doing something wrong or is this a major bug in Kryo?
Full example code:
The text was updated successfully, but these errors were encountered: