@JsonTypeInfo没有添加" @ class"包装类时,id到json也有@JsonTypeInfo

时间:2017-01-21 00:58:17

标签: java json serialization jackson

我试图序列化一个"持有者"具有值的类,其类型派生自泛型。 ie" class HasValue"有一个"私人A值"在里面。

此外,我还有界面" Inf"和一个具体的课程" InfCls"实现该接口。界面用@JsonTypeInfo标记,并被告知要创建一个" @ class"领域。

如果持有者类没有@JsonTypeInfo,那么一切正常。但是,当我补充说它破裂了。

注意:虽然在这个实例中我不需要@JsonTypeInfo(因为持有者总是一个具体的类),但是在需要时会出现同样的问题所以我选择这个作为一个更简单的例子。

以下SCWE:

import static org.junit.Assert.assertEquals;

import java.util.Objects;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SerializationIssues {

    // No @JsonTypeInfo on HasValue
    @Test
    public void json_HasValue_Inf_javaType() throws Exception {
        HasValue<Inf> object = new HasValue<>(new InfCls(1));

        ObjectMapper objectMapper = new ObjectMapper();
        JavaType javaType = objectMapper.getDeserializationConfig().getTypeFactory().constructParametricType(HasValue.class, Inf.class);
        String serialized = objectMapper.writerFor(javaType).writeValueAsString(object);
        HasValue<?> deserialized = objectMapper.readValue(serialized, javaType);

        assertEquals(object, deserialized);
    }

    // @JsonTypeInfo on HasValueId
    @Test
    public void json_HasValueId_Inf_javaType() throws Exception {
        HasValueId<Inf> object = new HasValueId<>(new InfCls(1));

        ObjectMapper objectMapper = new ObjectMapper();
        JavaType javaType = objectMapper.getDeserializationConfig().getTypeFactory().constructParametricType(HasValueId.class, Inf.class);
        String serialized = objectMapper.writerFor(javaType).writeValueAsString(object);
        HasValueId<?> deserialized = objectMapper.readValue(serialized, javaType);

        assertEquals(object, deserialized);
    }

    // ===== An interface and impl to test @JsonTypeInfo =====
    @JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = As.PROPERTY, property = "@class")
    public static interface Inf {
    }

    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, isGetterVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
    public static class InfCls implements Inf {
        private int val;

        @JsonCreator
        public InfCls(@JsonProperty("val") int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "InfCls [val=" + val + "]";
        }

        @Override
        public int hashCode() {
            return 31 * 1 + val;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            InfCls other = (InfCls) obj;
            if (val != other.val)
                return false;
            return true;
        }
    }

    // ===== A class that has a value =====
    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, isGetterVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
    public static class HasValue<A> {
        private final A val;

        @JsonCreator
        public HasValue(@JsonProperty("val") A val) {
            this.val = Objects.requireNonNull(val);
        }

        public A get() {
            return val;
        }

        @Override
        public String toString() {
            return "HasValue [val=" + val + "]";
        }

        @Override
        public int hashCode() {
            return 31 * 1 + ((val == null) ? 0 : val.hashCode());
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            HasValue<?> other = (HasValue<?>) obj;
            if (val == null) {
                if (other.val != null)
                    return false;
            } else if (!val.equals(other.val))
                return false;
            return true;
        }
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, isGetterVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
    public static class HasValueId<A> {
        private final A val;

        @JsonCreator
        public HasValueId(@JsonProperty("val") A val) {
            this.val = Objects.requireNonNull(val);
        }

        public A get() {
            return val;
        }

        @Override
        public String toString() {
            return "HasValue [val=" + val + "]";
        }

        @Override
        public int hashCode() {
            return 31 * 1 + ((val == null) ? 0 : val.hashCode());
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            HasValueId<?> other = (HasValueId<?>) obj;
            if (val == null) {
                if (other.val != null)
                    return false;
            } else if (!val.equals(other.val))
                return false;
            return true;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

一旦遇到@JsonTypeInfo注释,您正在引导的序列化程序就会被推开,并且您提供的类型信息将丢失。

有几种方法可以解决这个问题。您可以在课程中提供明确的类型信息:

public static class HasValueId<A extends Inf> {
    private final A val;

这将允许杰克逊通过内省确定字段类型并找到第二个注释。

或者您可以使用@JsonTypeInfo注释通用字段:

public static class HasValueId<A> {
    @JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = As.PROPERTY, property = "@class")
    private final A val; 

...这将消除界面上注释的需要。

完成后,您将不再需要输入您的作家;普通的旧objectMapper.writeValueAsString(object);将起作用。