Serialized polymorphic json for a specific jsonnode

I have a list of abstract class

List<Messaging> data;

Messages are an abstract class, and 3 more classes extend this class.

For instance,

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

I add all the messages in the list above

I used @JsonTypeInfo for the message class, as shown below:

@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")})

When I serialize this Java object to JSON using jackson, I get the values ​​as shown below:

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

I need my value as below:

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

How to serialize a list of polymorphic object to different node?

+4
source share
1 answer

Change JsonTypeInfoto include as WRAPPER_OBJECT.

eg:.

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

Source: https://habr.com/ru/post/1684280/


All Articles