簡體   English   中英

序列化多態json到特定的jsonnode

[英]serialise polymorphic json to specific jsonnode

我有抽象類的列表

List<Messaging> data;

Messaging是一個抽象類,另外3個類正在擴展這個類。

例如,

   Message class extends Messaging
   Product class extends Messaging
   Filter class extends Messaging

我正在添加上面列表中的所有消息

我在Messaging類上使用了@JsonTypeInfo,如下所示:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({  
    @Type(value = Message.class, name = "message"),  
    @Type(value = Filter.class, name = "filter"),
    @Type(value = Product.class , name = "product")})

當我使用jackson將此Java對象序列化為JSON時,我得到如下值:

   {
        "@type": "message",
        "type": "text",
        "value": "Is this the information you need"
    }

我想要的價值如下:

"message" : {
        "type": "text",
        "value": "Is this the information you need"
}

如何將多態對象列表序列化到不同的節點?

JsonTypeInfo更改為包含為WRAPPER_OBJECT

例如:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include=As.WRAPPER_OBJECT)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM