如果你有一个班级User:
class User {
private String firstName;
private String lastName;
private Address address; //object
}
和一个类Address分别:
class Address {
private String streetName;
private String postCode;
private AddressType addressType; //enum
}
以及为两者定制的 Jackson 反序列化器,是否有链接它们的好方法?例如
class UserDeserialiser extends JsonDeserializer<User> {
public User deserialize(JsonParser jp, DeserializationContext ctxt) {
ObjectNode node = jp.getCodec().readTree(jp);
User user = fetchUser();
user.setFirstName(node.get("firstName").asText());
user.setFirstName(node.get("lastName").asText());
user.setAddress(???); // delegate to AddressDeserialiser here
}
}
同样在地址反序列化器中,是否可以委托默认的来处理枚举(因为它可能具有自定义映射)?
class AddressDeserialiser extends JsonDeserializer<Address> {
public User deserialize(JsonParser jp, DeserializationContext ctxt) {
ObjectNode node = jp.getCodec().readTree(jp);
Address user = fetchAddress();
user.setStreetName(node.get("streetName").asText());
user.setAddressType(???); // delegate to jackson default object mapper?
}
}
相关分类