I have a class that contains a map (with a non String key) and some other fields.
public class MyClass() { private Map<KeyObject, OtherObject> map; private String someField; public MyClass(Map<KeyObject, OtherObject> map, String someField) { this.map = map; this.someField = someField; }
I would like to serialize and deserialize this class using Jackson. I saw different ways to do this and decided to try using jackson modules .
I followed this post and expanded on JsonDeserializer and JsonSerializer. The problem is that these classes must be printed, so it should look like
public class keyDeserializer extends JsonDeserializer<Map<KeyObject, OtherObject>> { ... }
The same goes for KeySerializer.
Then adding to the module:
module.addSerializer(new keySerializer()); module.addDeserializer(Map.class, new keyDeserializer());
But this is wrong because I get an exception:
keySerializer does not define valid handledType() -- must either register with method that takes type argument or make serializer extend 'org.codehaus.jackson.map.ser.std.SerializerBase'
I could use my serializer and deserializer for MyClass , but then I had to manually parse all this, which is not reasonable.
UPDATE:
I managed to get around creating a module in code using annotations
@JsonDeserialize(using = keyDeserializer.class) @JsonSerialize(using = keySerializer.class) private Map<KeyObject, OtherObject> map;
But then I have to serialize / deserialize the entire map structure myself from the output of toString (). Therefore, I tried another annotation:
@JsonDeserialize(keyUsing = MyKeyDeserializer.class) private Map<KeyObject, OtherObject> map;
Where MyKeyDeserializer extends org.codehaus.jackson.map.KeyDeserializer and overrides the method
public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException, JsonProcessingException {...}
Then manually deserialize my key, but again from the output of toString () of my key class.
This is not optimal (this depends on the toString () method). Is there a better way?