英文:
Deserialize multiple json keys to one field with Jackson
问题
以下是要翻译的代码部分:
我正在调用一个返回JSON响应的REST API,响应如下所示:
{
"data": {
"code": {
"code1": {
"name": "some-name",
"value": "0.25"
},
"code2": {
"name": "some-name2",
"value": "0.88"
},
"code3": {
"name": "some-name3",
"value": "0.00"
},
"code4": {
"name": "some-name4",
"value": "-0.11"
},
"code5": {
"name": "some-name5",
"value": "0.99"
}
}
}
}
我将其建模为以下的POJO类(为了清晰起见,我省略了“Data”类):
@Data
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class Code {
@JsonAlias({"code1", "code2", "code3", "code4", "code5"})
private final CustomCode customCode;
}
这对于“code”下面只有一个对象的情况正常工作。在上面的示例中,对于code1到code5,CustomCode只会有一个“codeX”对象,其中X是1、2、3、4或5。
我知道我可以使用Map,但如果可能的话,我想避免使用Map。我希望找到的是像这样的东西:
@Data
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class Code {
@JsonAlias({"code1", "code2", "code3", "code4", "code5"})
private final List<CustomCode> customCodeList;
}
其中`customCodeList`将保存每个code1到code5的POJO对象。
因此,我的问题是:Jackson是否提供了一些注释/配置,允许我们实现这一点?
英文:
I am calling a REST api that returns a json response like:
{
"data": {
"code": {
"code1": {
"name": "some-name",
"value": "0.25"
},
"code2": {
"name": "some-name2",
"value": "0.88"
},
"code3": {
"name": "some-name3",
"value": "0.00"
},
"code4": {
"name": "some-name4",
"value": "-0.11"
},
"code5": {
"name": "some-name5",
"value": "0.99"
}
}
}
}
And I modeled this to the following POJO (for clarity, I omit Data
class):
@Data
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class Code {
@JsonAlias({"code1", "code2", "code3", "code4", "code5"})
private final CustomCode customCode;
}
This works fine if there is only one object below "code". In the example above, with code1 to code5 CustomCode will only have one of the "codeX" objects - where X is 1, 2, 3, 4, or 5.
I know I could use a Map but I would like to avoid using a map if possible. What I look forward to find is something like:
@Data
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class Code {
@JsonAlias({"code1", "code2", "code3", "code4", "code5"})
private final List<CustomCode> customCodeList;
}
Where customCodeList
will hold each code1 to code5 pojos.
So my question is: does Jackson offer some annotation/configuration that allows us to accomplish this?
答案1
得分: 1
你可以像这样使用@JsonAnySetter
:
public static class Code {
private final List<CustomCode> customCodeList = new ArrayList<>();
private static final Set<String> ALIASES = ImmutableSet.of("code1", "code2", "...");
@JsonAnySetter
private void set(String name, CustomCode value) {
if (ALIASES.contains(name)) {
customCodeList.add(value);
}
}
}
这种方法的一个缺点是,如果你的Code
对象可能带有未知字段,你可能需要将set
的第二个参数更改为Object
,并且需要更仔细地解析它。
英文:
You could make use of @JsonAnySetter
like this:
public static class Code {
private final List<CustomCode> customCodeList = new ArrayList<>();
private static final Set<String> ALIASES = ImmutableSet.of("code1", "code2", "...");
@JsonAnySetter
private void set(String name, CustomCode value) {
if (ALIASES.contains(name)) {
customCodeList.add(value);
}
}
}
One downside of this approach is that if your Code
object may arrive with unknown fields, you might need to change the second parameter of set
to Object
and parse it more carefully.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论