英文:
Jackson. Deserialeze lists with different generic types
问题
以下是翻译好的内容:
我想要使用 Jackson 库进行自定义反序列化 List<Contact<Contact>>。
List<<Document>Document> 和 List<<Good>Good> 应该默认进行反序列化。
这是我的类:
public class Application implements Serializable {
// 其他字段
private List<Document> documents;
private List<Contact> contacts;
private List<Good> goods;
}
但我无法更改它以添加注解。所以我使用了 mixin:
public abstract class ApplicationMixin {
@JsonProperty("contactId")
private List<Contact> contacts;
}
自定义反序列化器:
@JsonComponent
public class ContactsDeserializer extends JsonDeserializer<List<Contact>> {
@Override
public List<Contact> deserialize(JsonParser parser, DeserializationContext context) throws IOException {
List<Contact> contacts = new ArrayList<>();
// 一些逻辑
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<Document> documents;
private List<Contact> contacts;
private List<Good> goods;
}
But I can't change it to add annotations. So I use mixin
public abstract class ApplicationMixin {
@JsonProperty("contactId")
private List<Contact> contacts;
}
Custom deserializer:
@JsonComponent
public class ContactsDeserializer extends JsonDeserializer<List<Contact>> {
@Override
public List<Contact> deserialize(JsonParser parser, DeserializationContext context) throws IOException {
List<Contact> contacts = new ArrayList<>();
//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<Contact>
、List<Document>
或任何其他 List 之间的区别,因此它将 ContactsDeserializer 应用于所有这些类型。您需要更精确地说明应该将反序列化器应用于哪些地方。
您可以尝试以下操作:
- 从
ContactsDeserializer
中移除@JsonComponent
注解。 - 在
ApplicationMixin
中,为contacts
字段添加@JsonDeserialize(using = ContactsDeserializer.class)
。
public abstract class ApplicationMixin {
@JsonProperty("contactId")
@JsonDeserialize(using = ContactsDeserializer.class)
private List<Contact> contacts;
}
或者,您可以考虑为 Contact
类创建一个自定义的反序列化器,而不是为 List<Contact>
创建。是否可能取决于输入的 JSON 格式。
英文:
The observed behavior is caused by the type erasure. On runtime, Jackson cannot tell the difference between List<Contact>
, List<Document>
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:
- remove
@JsonComponent
annotation from theContactsDeserializer
- in the
ApplicationMixin
, add@JsonDeserialize(using = ContactsDeserializer.class)
to thecontacts
field.
public abstract class ApplicationMixin {
@JsonProperty("contactId")
@JsonDeserialize(using = ContactsDeserializer.class)
private List<Contact> contacts;
}
Alternatively, you might consider creating a custom deserializer for the Contact
class instead of the List<Contact>
. If it's possible depends on the input JSON format.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论