将Jackson字段序列化为JSON字符串(而不是对象)的注解

igetnqfo  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(186)

我正在使用Amazon SNS推送通知,它们要求我发送如下有效载荷:

{ 
  "default": "This is the default message which must be present when publishing a message to a topic. The default message will only be used if a message is not present for 
one of the notification platforms.",     
  "APNS": "{\"aps\":{\"alert\": \"Check out these awesome deals!\",\"url\":\"www.amazon.com\"} }",
  "GCM": "{\"data\":{\"message\":\"Check out these awesome deals!\",\"url\":\"www.amazon.com\"}}",
  "ADM": "{\"data\":{\"message\":\"Check out these awesome deals!\",\"url\":\"www.amazon.com\"}}" 
}

我有一些类,看起来像:

record APS(String alert) {}
record APNS(APS aps) {}
record SNSNotification(
  @JsonProperty("default") String defaultMessage,
  @JsonProperty("APNS") APNS apns
) {}

但是当我序列化它们时,我得到的是正常的JSON:

{
  "default": "...",
  "APNS": { "alert": "..." }
}

我考虑过使用@JsonSerialize(as = String.class, using = SomeSerializer.class),但我不确定要在那里插入什么类?BeanSerializer不喜欢我使用一个记录,其他什么都没有真正意义。
有没有一种简单的方法可以在不预序列化字段的情况下完成此操作?

qojgxg4l

qojgxg4l1#

我研究了使用@JsonSerialize(作为=字符串.类,使用= SomeSerializer.类)
其中一个选项是通过扩展JsonSerializer并重写其serialize()方法来创建自定义序列化程序。
serialize()的一个参数是JsonGenerator,顾名思义,它允许生成JSON。您可以在相当低的级别上使用JsonGenerator,通过手动指定JSON对象(JSON数组)的开始/结束,写入字段名等,或者您可以将嵌套对象原样馈送到JsonGenerator中,这取决于它的默认形状是否满足您的需要。
这就是它看起来的样子(* 我没有在JSON中使用“GCM”和“ADM”属性,因为不清楚它们应该来自哪里 *)。

@JsonSerialize(using = SNSNotificationSerializer.class)
record SNSNotification(
    @JsonProperty("default") String defaultMessage,
    @JsonProperty("APNS") APNS apns
) {}

public class SNSNotificationSerializer extends JsonSerializer<SNSNotification> {
    @Override
    public void serialize(SNSNotification value,
                          JsonGenerator gen,
                          SerializerProvider serializers) throws IOException {
        
        gen.writeStartObject();     // {
        gen.writeStringField("default", value.defaultMessage()); // "default": " ",
        gen.writeFieldName("APNS"); // "APNS" :
        gen.writeStartObject();     // {
        gen.writeFieldName("aps");  // "aps" :
        gen.writeStartObject();     // {
        gen.writeStringField("alert", value.apns().aps().alert()); // "alert" : " ",
        gen.writeStringField("url", "www.amazon.com");             // "url":"www.amazon.com"
        gen.writeEndObject();       // }
        gen.writeEndObject();       // }
        gen.writeEndObject();       // }
    }
}
  • 用法示例:*
SNSNotification snsNotification = new SNSNotification(
    "This is the default message which must be present when publishing a message to a topic. The default message will only be used if a message is not present for one of the notification platforms.",
    new APNS(new APS("Check out these awesome deals!"))
);
        
ObjectMapper mapper = new ObjectMapper();
        
String json = mapper
    .writerWithDefaultPrettyPrinter()
    .writeValueAsString(snsNotification);
    
System.out.println(json);
  • 输出:*
{
  "default" : "This is the default message which must be present when publishing a message to a topic. The default message will only be used if a message is not present for one of the notification platforms.",
  "APNS" : {
    "aps" : {
      "alert" : "Check out these awesome deals!",
      "url" : "www.amazon.com"
    }
  }
}

相关问题