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 9182559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:35:17+00:00 2026-06-17T18:35:17+00:00

How do i tell jackson to use a different serializer for each of the

  • 0

How do i tell jackson to use a different serializer for each of the Generic collection type.
I have a CustomProductsSerializer and a CustomClientsSerializer

    public class ProductsListSerializer extends JsonSerializer<List<Products>>{

    // logic to insert __metadata , __count into json
    public void serialize(List<Products> value, JsonGenerator jgen, SerializerProvider provider) {
     .... .... .... 
     }
    // always returns ArrayList.class 
    public Class<List<Instrument>> handledType() {
    Class c = var.getClass();
    return c;
    }
    }


    public class CustomClientsSerializer extends JsonSerializer<List<Clients>>{

    // logic to insert __metadata , __count into json
    public void serialize(List<Clients> value, JsonGenerator jgen, SerializerProvider provider){
     .... .... .... 
     }
     .... .... .... 
    // always returns ArrayList.class 
    public Class<List<Clients>> handledType() {
    Class c = var.getClass();
    return c;
    }
    }

I have registered both the serializers using SimpleModule.
The problem is, since handledType in both cases returns ArrayList.class, serialization of clients below fail with a class cast exception.

    List<Clients> clients;
    // code to get clients from database. 
    String jsonString = mapper.writeValueAsString(clients);

Question is how to tell jackson about which serializer to use ?

I am using jackson as the json serializer with resteasy.

–Edit in response to @HiJon89
I have a business object which returns ‘List’ and another which returns List. The way I need is when the Busines Object returns a ‘List’ then ProductsListSerializer should be used for the whole List. and When Business Object returns ‘List the CustomClientsSerializer should be used for the whole List. In my use-case I append additional elements to the serialized json ‘eg:__count, __metadata , __references ‘ There is only one __count per Collection. Contentusing Can only be used on properties. Any suggestions ?

  • 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-17T18:35:18+00:00Added an answer on June 17, 2026 at 6:35 pm

    It possible by extending the simplemodule or by extending com.fasterxml.jackson.databind.Module. I have taken the second approach. Below are the summary of changes

    added method to take JavaType as parameter.

        public <T> TypedModule addSerializer(JavaType type, JsonSerializer<T> ser)
    {
        if (_serializers == null) {
            _serializers = new TypedSerializers();
        }
        _serializers.addSerializer(type, ser);
        return this;
    }
    

    Above method adds the new serilaizer to

    protected TypedSerializers _serializers = null;
    

    The class ‘TypedSerializers extends Serializers.Base’ stores all the typed serializers in a HashMap.

    Changed the logic of findSerializer method in TypedSerializers

    public JsonSerializer<?> findSerializer(SerializationConfig config,
            JavaType type, BeanDescription beanDesc) {
    
        Class<?> cls = type.getRawClass();
        ClassKey key = new ClassKey(cls);
        JsonSerializer<?> ser = null;
        if (_javaTypeMappings != null) {
            ser = _javaTypeMappings.get(type.toString());
            if (ser != null) {
                return ser;
            }
        }
     ..... process normal serilaizers ...
    

    Below change is needed when registering a customserializer

        TypedModule simpleModule = new TypedModule("TypedModule", new Version(1, 0, 0, null));       
        JavaType productlist = this.getTypeFactory().constructCollectionType(List.class, Product.class);
        JavaType clientlist = this.getTypeFactory().constructCollectionType(List.class, Client.class);
        simpleModule.addSerializer(productlist,  new prodctlistserializer());
        simpleModule.addSerializer(clientlist, new clientlistserializer() );
        this.registerModule(simpleModule);
    

    The resulting behaviour is, when ‘List of type Product’ is passed to ObjectMapper , it tries to locate a suitable serilaizer. We are maintaining the generic serializers in the map of _javaTypeMappings. The Objecmapper executes findSerializer. The findSerializer first checks for an avaliable serializer in _javaTypeMappings. In this case it returns prodctlistserializer. When ‘List of type Client ‘ is passed it returns clientlistserializer.

    I have taken all source code under com.fasterxml.jackson.databind.Module to a package in my project and added above changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tell me please, how to use getBulkUserInformationFor: method correctly? I have Could not authenticate
How do I tell Jackson to ignore JSON name? I have the following POJO:
I'm experementing with Jackson serialization/deserialization. For instance, I have such class: class Base{ String
Is there any way I can tell Jackson to ignore properties from parent class
I have a class that needs to be deserialized from JSON using Jackson. The
I am trying to get to grips with Jackson JSON parser and have found
Please tell me whats wrong with this c# code.. public bool CloseCOMPort() { try
Please tell me what is use of: <add assembly=*/> because of this i am
Tell me if my concept is wrong. I have 2 classes; Country and State
Tell me, how can I serialize/deserialize the fusion::vector object type? Thanks.

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.