2017-01-16 96 views
0

我正在解析类为JSON Apple Push Notification Service。所以我有一个ApnsPayload类,并希望将最终的JSON附加到额外的,可选的和动态的acme属性。这意味着我需要将以下杰克逊输出转换...Jackson Json序列化程序追加内部属性

{ 
    "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" 
} 

我已经成功添加一个前缀acme-那些键与@JsonNaming(),但我不能搬完属性一级。请帮忙,谢谢!

@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; 
     } 
    } 
} 

回答

1

大声笑,我发现我的答案张贴这个问题在2分钟后。我需要的是Jackson Unwrapping Feature,将@JsonUnwrapped添加到要解包的房产。

@JsonUnwrapped 
private Acme acme; 
相关问题