I have a POJO in my Java project which has a field with datatype Map<String, Object>
:
@Data
@NoArgsConstructor
public class B {
private Map<String,Object> dummyField;
}
Currently, while serializing the JSON using the Jackson the field has been written as:
"dummyField" : {
"Google" : "https://google.com",
"yt" : "https://yt.com"
}
I would like to know if there is a way in Jackson to serialize this field something like this,
"dummyField" : [
{
"Google" : "https://google.com"
},
{
"yt" : "https://yt.com"
}
]
I am aware that I can achieve this using the custom serializer. I was hoping to know if there is any direct way for Jackson to achieve this.
According to default implementation of MapSerializer it creates new JSON object
gen.writeStartObject(value)
always. There is no ability to configure array creation instead.Default implemetation of
MapSerializer
:The custom serializer is more efficient solution.
But the alternative solution to achieve such serialization - is simply to convert
Map
to theSet<Map.Entry>
Solution 1,
@JsonGetter
with converting Map->SetSolution 2, custom type converter Map->Set