Is it possible to tell the JsonGenerator
to serialize an object using a specific JsonView?
For example:
@Data
class Author {
@JsonSerialize(using=CustomSerializer.class)
List<Book> bestFiveBooksOfAllTime;
};
@Data
class Book extends BaseEntity
{
@JsonView(SummaryView.class)
private String title;
@JsonView(SummaryView.class)
private String author;
private String review;
}
In my CustomSerializer
, I'd like to be able to do something along the lines of:
public class CustomSerializer extends JsonSerializer<Author> {
@Override
public void serialize(Author value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeStartObject();
for (Book book : value.getBestFiveBooksOfAllTime()) {
// Something like the following...
jgen.writeObjectFieldUsingView(book.getId().toString(), book, SummaryView.class);
}
jgen.writeEndObject();
}
}
Ie., in the above scenario, I'd like the result to contain a map of bestFiveBooksOfAllTime
, where the value is just the SummaryView
of the book.