gpt4 book ai didi

java - Jackson Json 序列化器附加内部属性

转载 作者:行者123 更新时间:2023-12-01 09:00:55 25 4
gpt4 key购买 nike

我正在将 Apple Push Notification Service 的类解析为 JSON 。因此,我有一个 ApnsPayload 类,并希望在最终的 JSON 中附加额外的、可选的动态 acme 属性。这意味着我想转换以下 Jackson 输出...

{
"aps": {
"alert": {
"body": "body",
"title": "title"
},
"badge": 123,
"category": "category"
},
"acme": {
"1": "1",
"value1": "value1"
}
}

对此...

{
"aps": {
"alert": {
"body": "body",
"title": "title"
},
"badge": 123,
"category": "category"
},
"acme-1": "1",
"acme-value1": "value1"
}

我已成功使用 @JsonNaming() 将前缀 acme- 添加到这些键,但是我无法将这些属性移动一级。请帮忙,谢谢!

@Value
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
// need a serializer to move up properties in Acme by one level
// @JsonSerialize(using = NewSerializer.class)
public class ApnsPayload {

private Aps aps;

private Acme acme;

@JsonNaming(AcmeNamingStrategy.class) // add prefix acme-
public interface Acme {
}

@Value
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class Aps {

private Alert alert;

private Integer badge;

private String sound;

@JsonProperty("content-available")
private Integer contentAvailable;

private String category;

@JsonProperty("thread-id")
private String threadId;

@Value
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class Alert {

private String title;
private String body;

@JsonProperty("title-loc-key")
private String titleLocalizationKey;

@JsonProperty("title-loc-args")
private List<String> titleLocalizationArgs;

@JsonProperty("action-loc-key")
private String actionLocalizationKey;

@JsonProperty("loc-key")
private String localizationKey;

@JsonProperty("loc-args")
private List<String> localizationArgs;

@JsonProperty("launch-image")
private String launchImage;
}
}
}

最佳答案

哈哈,我在发布这个问题后两分钟就找到了答案。我所需要的只是 Jackson Unwrapping Feature ,通过将 @JsonUnwrapped 添加到要解包的属性。

@JsonUnwrapped
private Acme acme;

关于java - Jackson Json 序列化器附加内部属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41671409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com