We have the following code:
@JsonDeserialize(keyUsing = DomainInstanceDeserializer.class, contentUsing = FactDeserializer.class)
private final Map<DomainInstance, Fact> facts;
The DomainInstanceDeserializer
has the following code in place:
public class DomainInstanceDeserializer extends KeyDeserializer {
.
.
.
@Override
public Object deserializeKey(String domainInstanceAsString, DeserializationContext deserializationContext) {
The FactDeserialzer
has the following code in place:
public class FactDeserializer extends JsonDeserializer<Fact> {
.
.
.
@Override
public Fact deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
JsonNodeType nodeType = node.getNodeType();
switch (nodeType) {
As it can be seen for the Fact
we can use the object type from the node to see of which type it is.
Can this be done on the key, for the DomainInstance
?
We use the annotation keyUsing
which implies using the KeyDeserializer
class. Is there another class that can be used on the key for the map?
Since Map
in JSON
is represented as JSON Object
which always has keys as string
you can not expect there anything else then String
.
You need to encode type manually. For example, you can use underscores: type_value1_value2_value3
and decode it in KeyDeserializer
.