我想配置一个杰克逊反序列化器,根据带注释的字段的目标类型,它的行为不同。
public class Car {
@JsonSerialize(using=IdSerializer.class)
@JsonDeserialize(using=IdDeserializer.class)
String id
}
public class Bus {
@JsonSerialize(using=IdSerializer.class)
@JsonDeserialize(using=IdDeserializer.class)
Id id
}
Jackson 序列化程序知道它转换数据的类型,所以这是有效的:
public class IdSerializer extends JsonSerializer<Object> {
@Override
public void serialize(Object value, JsonGenerator jsonGen, SerializerProvider provider) throws IOException {
// value is the annotated field class
if(value instanceof String)
jsonGen.writeObject(...);
else if (value instanceof Id)
jsonGen.writeObject(...);
else
throw new IllegalArgumentException();
}
}
Jackson 反序列化器似乎不知道它将数据转换成的目标类型:
public class IdDeserializer extends JsonDeserializer<Object> {
@Override
public Object deserialize(JsonParser jp, DeserializationContext context) throws IOException {
// what is the annotated field class?
}
}