-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ObjectMapper.readValue fails for TreeSet
for byte[] (and other types that are not Comparable
)
#255
Comments
Interesting. One work-around that might work (but one I have not tried) would be to use 'updating reader':
in which case existing map will be used. I will see if I can find a fix for the underlying issue. |
Thank you very much.
But the following code would be the best we think (if it can work).
|
Oh. There is actually something that should help you solve your problem. Just create a sub-class like:
and then either use this type instead of TreeSet<byte[]>. This would allow proper deserialization. |
Thanks, we have changed the solution to the one you provided, it's more effective than ours. |
Ok good that it works. I hope that eventually there could be better solutions; I will edit title of this entry a bit, but leave it open. |
hi, cowtowncoder
The demo source is below: import java.util.NavigableSet;
import java.util.TreeSet;
import org.apache.hadoop.hbase.util.Bytes;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
class MyByteArraySet extends TreeSet<byte[]> {
public MyByteArraySet() {
super(Bytes.BYTES_COMPARATOR);
}
}
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
NavigableSet<byte[]> set = new TreeSet<>(Bytes.BYTES_COMPARATOR);
set.add(Bytes.toBytes("ROW1"));
set.add(Bytes.toBytes("ROW2"));
set.add(Bytes.toBytes("ROW3"));
byte[] array = mapper.writeValueAsBytes(set);
NavigableSet<byte[]> result = mapper.readValue(array,
new TypeReference<MyByteArraySet>() {
});
for (byte[] value : result) {
System.out.println(Bytes.toString(value));
}
}
} Any idea about this? |
Oh that's simple. Your inner class must have |
Related issue: #509. |
Actually I think that #1399 is a solution for this: this does require setting of initial value (and either global default for merging, or per-property annotation), but should work fine. |
We use jackson-databind-2.2.2 to serialize and deserialize NavigableSet<byte[]>.
Serializing works fine, but deserializing throws java.lang.ClassCastException:
Below is the code:
It seems that we need provide a Comparator parameter(such as Bytes.BYTES_COMPARATOR) when we create a new TreeSet object, but how can we do that in TypeReference when deserializing by mapper.readValue method.
The text was updated successfully, but these errors were encountered: