带杰克逊多晶型处理的定制串行器

时间:2018-06-07 11:12:08

标签: java json jackson

我遇到了杰克逊Polymorphic Type Handling功能的问题。

我有以下类的结构:



    @JsonAutoDetect(
        getterVisibility = NONE,
        isGetterVisibility = NONE,
        setterVisibility = NONE
    )
    public class Boundary<T> {
        @JsonProperty(value = "value", required = true)    
        @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT)
        private final T value;

        @JsonProperty(value = "inclusive", required = true)
        private final boolean inclusive;

        @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
        public Boundary(@JsonProperty(value = "value", required = true) T value,
                        @JsonProperty(value = "inclusive", required = true) boolean inclusive) {
            this.value = value;
            this.inclusive = inclusive;
        }

        public static <T> Boundary<T> inclusive(T value) {
            return new Boundary<>(value, true);
        }

        public static <T> Boundary<T> exclusive(T value) {
            return new Boundary<>(value, false);
        }    

        public T getValue() {
            return value;
        }

        public boolean isInclusive() {
            return inclusive;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Boundary<?> boundary = (Boundary<?>) o;
            return inclusive == boundary.inclusive &&
            Objects.equals(value, boundary.value);
        }

        @Override
        public int hashCode() {
            return Objects.hash(value, inclusive);
        }
    }



    public interface BaseRange<T> {
        Boundary<T> getFrom();

        Boundary<T> getTo();
    }



    @JsonAutoDetect(
            getterVisibility = NONE,
            isGetterVisibility = NONE,
            setterVisibility = NONE
    )
    public class QuantityRange implements BaseRange<Quantity> {
        @JsonProperty(value = "from")
        private Boundary<Quantity> from;

        @JsonProperty(value = "to")
        private Boundary<Quantity> to;

        public QuantityRange() {
        }

        public QuantityRange(Boundary<Quantity> from, Boundary<Quantity> to) {
            this.from = from;
            this.to = to;
        }

        @Override
        public Boundary<Quantity> getFrom() {
            return from;
        }

        public void setFrom(Boundary<Quantity> from) {
            this.from = from;
        }

        @Override
        public Boundary<Quantity> getTo() {
            return to;
        }

        public void setTo(Boundary<Quantity> to) {
            this.to = to;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            QuantityRange that = (QuantityRange) o;
            return Objects.equals(from, that.from) && Objects.equals(to, that.to);
        }

        @Override
        public int hashCode() {
            return Objects.hash(from, to);
        }
    }

Quantity类的内容与问题无关,但有一个重要说明:Quantity类是共享库的成员,我无法更改源代码(例如,添加一些杰克逊注释。

但我在SimpleModule注册了QuantitySerializer个自定义QuantityDeserializerObjectMapper,如下所示:



    ObjectMapper mapper = new ObjectMapper()            
        .registerModule(new SimpleModule()
                .addSerializer(Quantity.class, new QuantitySerializer())
                .addDeserializer(Quantity.class, new QuantityDeserializer())
        );

serializeWithType中的覆盖方法QuantitySerializer,一切都像魅力一样,直到我需要为Quantity提供序列化QuantityRange的替代序列化工具。

所以,我的问题是:如何配置Jackson为Quantity类使用特殊的序列化程序来序列化QuantityRange,但对于类型为{的字段的所有其他类继续使用标准QuantitySerializer {1}}?

我无法添加注释,如

Quantity

它只是赢了工作。

0 个答案:

没有答案