'Jackson deserialization of interface affects constituent member's deserializaton

I have an interface type with many implementation classes. I can serialize/deserialize this either by

@JsonDeserialize(using = AlarmStateDeserializer.class)
@JsonSerialize(using = AlarmStateSerializer.class)
@JsonTypeInfo( use = JsonTypeInfo.Id.NONE)
public interface AlarmState
{
  :

or

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = AcknowledgedState.class, name = "acknowledged"),
    @JsonSubTypes.Type(value = ActivatedState.class, name = "activated"),
    @JsonSubTypes.Type(value = DeactivatedState.class, name = "deactivated"),
    @JsonSubTypes.Type(value = BlockedState.class, name = "blocked"),
    @JsonSubTypes.Type(value = ReactivatedState.class, name = "reactivated"),
    @JsonSubTypes.Type(value = DisabledState.class, name = "disabled"),
    @JsonSubTypes.Type(value = NormalState.class, name = "normal"),
})

and that works fine when I seralize/deserialize the AlarmState directly in the ObjectMapper.

BUT, if I include the AlarmState as a property in another type, it insists to have a type for that field.

I have tried just about every other way I can think of, but for instance;

@JsonTypeInfo( use = JsonTypeInfo.Id.NONE )
@JsonDeserialize(as = AlarmImpl.class)
public interface Alarm
{
    AlarmState getState();
  :

(It doesn't matter if I deserialize the AlarmImpl directly either, same result)

And I will get the following exception;

Could not resolve subtype of [simple type, class com.sensetif.pipes.alarms.AlarmState]: missing type id property 'type' (for POJO property 'state')

I have also tried to put all kinds of annotations on the getState() method, but to no prevail.

FTR; the json in question is;

{
  "labels": [],
  "state": {
    "name": "normal",
    "description": "Normal state indicates everything is fine.",
    "creationTime": "1970-01-01T00:00:00Z"
  },
  "counter": 0,
  "name": "niclas1",
  "description": null,
  "condition": false,
  "alarmClass": "C",
  "organization": 0,
  "parameters": null
}

How can it be that the underlying type deserializes fine, but not when it is part of an outer object??



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source