java @JsonUnwrapped 的 Jackson 反序列化等价物是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16570073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the Hymanson deserialization equivalent of @JsonUnwrapped?
提问by Shaun
Say I have the following class:
假设我有以下课程:
public class Parent {
public int age;
@JsonUnwrapped
public Name name;
}
Producing JSON:
生成 JSON:
{
"age" : 18,
"first" : "Joey",
"last" : "Sixpack"
}
How do I deserialize this back into the Parent class? I could use @JsonCreator
我如何将它反序列化回父类?我可以使用@JsonCreator
@JsonCreator
public Parent(Map<String,String> jsonMap) {
age = jsonMap.get("age");
name = new Name(jsonMap.get("first"), jsonMap.get("last"));
}
But this also effectively adds @JsonIgnoreProperties(ignoreUnknown=true)
to the Parent class, as all properties map to here. So if you wanted unknown JSON fields to throw an exception, you'd have to do that yourself. In addition, if the map values could be something other than Strings, you'd have to do some manual type checking and conversion. Is there a way for Hymanson to handle this case automatically?
但这也有效地添加@JsonIgnoreProperties(ignoreUnknown=true)
到 Parent 类,因为所有属性都映射到这里。因此,如果您希望未知的 JSON 字段引发异常,则必须自己执行此操作。此外,如果映射值可能不是字符串,则您必须进行一些手动类型检查和转换。Hyman逊有没有办法自动处理这种情况?
Edit:I might be crazy, but this actually appears to work despite never being explicitly mentioned in the documentation: http://fasterxml.github.io/Hymanson-annotations/javadoc/2.2.0/com/fasterxml/Hymanson/annotation/JsonUnwrapped.html
I was pretty sure it didn't work for me previously. Still, the proposed @JsonCreator approach might be preferred when custom logic is required to deserialize unwrapped polymorphic types.
编辑:我可能疯了,但这实际上似乎有效,尽管文档中从未明确提及:http: //fasterxml.github.io/Hymanson-annotations/javadoc/2.2.0/com/fasterxml/Hymanson/annotation/ JsonUnwrapped.html
我很确定它以前对我不起作用。尽管如此,当需要自定义逻辑来反序列化解包的多态类型时,建议的 @JsonCreator 方法可能是首选。
采纳答案by hoaz
You can use @JsonCreator
with @JsonProperty
for each field:
您可以为每个字段使用@JsonCreator
with @JsonProperty
:
@JsonCreator
public Parent(@JsonProperty("age") Integer age, @JsonProperty("firstName") String firstName,
@JsonProperty("lastName") String lastName) {
this.age = age;
this.name = new Name(firstName, lastName);
}
Hymanson does type checking and unknown field checking for you in this case.
在这种情况下,Hymanson 会为您进行类型检查和未知字段检查。
回答by spinlok
It does work for deserialization as well, although it's not mentioned in the docs explicitly, like you said. See the unit test for deserialization of @JsonUnwrapped
here for confirmation - https://github.com/FasterXML/Hymanson-databind/blob/d2c083a6220f2875c97c29f4823d9818972511dc/src/test/java/com/fasterxml/Hymanson/databind/struct/TestUnwrapped.java#L138
它也适用于反序列化,尽管文档中没有明确提到它,就像你说的那样。请参阅@JsonUnwrapped
此处的反序列化单元测试以进行确认 - https://github.com/FasterXML/Hymanson-databind/blob/d2c083a6220f2875c97c29f4823d9818972511dc/src/test/java/com/fasterxml/Hymanson/databind/struct/java#LTest11
回答by Artem Areshko
@JsonUnwrapped
works for both serialization and deserialization, you shouldn't need to take any additional steps.
@JsonUnwrapped
适用于序列化和反序列化,您不需要采取任何额外的步骤。
回答by Jan Mares
For those who googled here like me, trying to resolve issue when deserializing unwrapepd Map
, there is a solution with @JsonAnySetter
:
对于像我这样在 google 上搜索的人,在反序列化 unwrapepd 时试图解决问题Map
,有一个解决方案@JsonAnySetter
:
public class CountryList
{
Map<String, Country> countries = new HashMap<>();
@JsonAnySetter
public void setCountry(String key, Country value)
{
countries.put(key, value);
}
}