Jackson. 使用不同泛型类型进行反序列化列表

huangapple go评论77阅读模式
英文:

Jackson. Deserialeze lists with different generic types

问题

以下是翻译好的内容:

我想要使用 Jackson 库进行自定义反序列化 List<Contact<Contact>>。
List<<Document>Document> 和 List<<Good>Good> 应该默认进行反序列化。

这是我的类:

public class Application implements Serializable {
    // 其他字段

    private List&lt;Document&gt; documents;
    private List&lt;Contact&gt; contacts;
    private List&lt;Good&gt; goods;
}

但我无法更改它以添加注解。所以我使用了 mixin:

public abstract class ApplicationMixin {
    @JsonProperty("contactId")
    private List&lt;Contact&gt; contacts;
}

自定义反序列化器:

@JsonComponent
public class ContactsDeserializer extends JsonDeserializer&lt;List&lt;Contact&gt;&gt; {
    @Override
    public List&lt;Contact&gt; deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        List&lt;Contact&gt; contacts = new ArrayList&lt;&gt;();

        // 一些逻辑

        return contacts;
    }
}

在反序列化过程中,所有的列表都尝试使用这个自定义反序列化器。
但我希望它仅用于 contacts 列表。
我该如何做到这一点?

英文:

I want deserialize List<Contact<Contact>> with custom deserialezer using jackson library.
List<<Document>Document> and List<<Good>Good> should deserialize by default.

There is my class

public class Application implements Serializable {
    //other fields

    private List&lt;Document&gt; documents;
    private List&lt;Contact&gt; contacts;
    private List&lt;Good&gt; goods;
}

But I can't change it to add annotations. So I use mixin

public abstract class ApplicationMixin {
	@JsonProperty(&quot;contactId&quot;)
	private List&lt;Contact&gt; contacts;
}

Custom deserializer:

@JsonComponent
public class ContactsDeserializer extends JsonDeserializer&lt;List&lt;Contact&gt;&gt; {
	@Override
	public List&lt;Contact&gt; deserialize(JsonParser parser, DeserializationContext context) throws IOException {
		List&lt;Contact&gt; contacts = new ArrayList&lt;&gt;();

        //some logic

       return contacts;
   }
}

And during deserialization all lists try to use this custom deserializer.
But I want it only for contacts.
What can I do for this?

答案1

得分: 0

观察到的行为是由type erasure引起的。在运行时,Jackson 无法区分 List&lt;Contact&gt;List&lt;Document&gt; 或任何其他 List 之间的区别,因此它将 ContactsDeserializer 应用于所有这些类型。您需要更精确地说明应该将反序列化器应用于哪些地方。

您可以尝试以下操作:

  1. ContactsDeserializer 中移除 @JsonComponent 注解。
  2. ApplicationMixin 中,为 contacts 字段添加 @JsonDeserialize(using = ContactsDeserializer.class)
public abstract class ApplicationMixin {
    @JsonProperty("contactId")
    @JsonDeserialize(using = ContactsDeserializer.class)
    private List<Contact> contacts;
}

或者,您可以考虑为 Contact 类创建一个自定义的反序列化器,而不是为 List&lt;Contact&gt; 创建。是否可能取决于输入的 JSON 格式。

英文:

The observed behavior is caused by the type erasure. On runtime, Jackson cannot tell the difference between List&lt;Contact&gt;, List&lt;Document&gt; or any other List, so it applies the ContactsDeserializer to all of them. You will need to be more precise about what the deserializer should be applied to.

You can try the following:

  1. remove @JsonComponent annotation from the ContactsDeserializer
  2. in the ApplicationMixin, add @JsonDeserialize(using = ContactsDeserializer.class) to the contacts field.
public abstract class ApplicationMixin {
    @JsonProperty(&quot;contactId&quot;)
    @JsonDeserialize(using = ContactsDeserializer.class)
    private List&lt;Contact&gt; contacts;
}

Alternatively, you might consider creating a custom deserializer for the Contact class instead of the List&lt;Contact&gt;. If it's possible depends on the input JSON format.

huangapple
  • 本文由 发表于 2020年10月17日 17:59:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64401211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定