Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:49:11+00:00 2026-06-16T00:49:11+00:00

Is it possible to tell the JsonGenerator to serialize an object using a specific

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:49:13+00:00Added an answer on June 16, 2026 at 12:49 am

    Here’s the solution I ended up using — please advise if there’s a better way

    @Override
    public void serialize(Author value, JsonGenerator jgen,
            SerializerProvider provider) throws IOException,
            JsonProcessingException {
    
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false);
        mapper.setSerializationConfig(mapper.getSerializationConfig().withView(SummaryView.class));
    
        jgen.writeStartObject();
        for (Book book : value.getBestFiveBooksOfAllTime()) {
            jgen.writeFieldName(book.getId().toString());
            mapper.writeValue(jgen, book);
        }
        jgen.writeEndObject();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to tell if a list is infinite? In Haskell, you can
In a web application using struts2 ejb hibernate, is it possible to tell the
Is it possible to tell which directory the user ran Ant from? For example,
My question is is it possible to tell from an UIEvent object whether it
Is it possible to tell the difference between the normal exit of a using
Is it possible to tell JAXB to create Hibernate bidirectional mapping? Holding class can
Example: ... Line some text other text 10 ... Is it possible to tell
I am using Delphi 2010. Is it possible to tell Delphi to not generate
i am using Linkify with a custom TransformFilter. Is it possible to tell linkify
Is it possible tell the JVM to hint the OS that a certain object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.