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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:22:24+00:00 2026-05-27T13:22:24+00:00

I’m experementing with Jackson serialization/deserialization. For instance, I have such class: class Base{ String

  • 0

I’m experementing with Jackson serialization/deserialization.
For instance, I have such class:

class Base{
    String baseId;
}

And I want to serialize List objs;
To do it with jackson, I need to specify a list’s elements real type, due to the java type erasure.
This code will work:

List<Base> data = getData();
return new ObjectMapper().writerWithType(TypeFactory.collectionType(List.class, Base.class)).writeValueAsString(data);

Now, I want to serialize more complex class:

class Result{
     List<Base> data;
}

How should I tell Jackson to properly serialize this class?

  • 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-05-27T13:22:25+00:00Added an answer on May 27, 2026 at 1:22 pm

    Just

    new ObjectMapper().writeValueAsString(myResult);
    

    The type of the list won’t be lost due to type erasure in the same way it would be in the first example.


    Note that for vanilla serialization of a list or generic list, it’s not necessary to specify the list component types, as demonstrated in the example in the original question. All three of the following example serializations represent the List<Bar> with the exact same JSON.

    import java.util.ArrayList;
    import java.util.List;
    
    import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
    import org.codehaus.jackson.annotate.JsonMethod;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.ObjectWriter;
    
    public class JacksonFoo
    {
      public static void main(String[] args) throws Exception
      {
        Baz baz = new Baz("BAZ", 42);
        Zab zab = new Zab("ZAB", true);
        List<Bar> bars = new ArrayList<Bar>();
        bars.add(baz);
        bars.add(zab);
    
        ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
    
        String json1 = mapper.writeValueAsString(bars);
        System.out.println(json1);
        // output:
        // [{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}]
    
        Foo foo = new Foo(bars);
    
        String json2 = mapper.writeValueAsString(foo);
        System.out.println(json2);
        // output:
        // {"bars":[{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}]}
    
        mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
        ObjectWriter typedWriter = mapper.writerWithType(mapper.getTypeFactory().constructCollectionType(List.class, Bar.class));
    
        String json3 = typedWriter.writeValueAsString(bars);
        System.out.println(json3);
        // output:
        // [{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}]
      }
    }
    
    class Foo
    {
      List<Bar> bars;
      Foo(List<Bar> b) {bars = b;}
    }
    
    abstract class Bar
    {
      String name;
      Bar(String n) {name = n;}
    }
    
    class Baz extends Bar
    {
      int size;
      Baz(String n, int s) {super(n); size = s;}
    }
    
    class Zab extends Bar
    {
      boolean hungry;
      Zab(String n, boolean h) {super(n); hungry = h;}
    }
    

    A typed writer is useful when serializing with additional type information. Note how the json1 and json3 outputs below differ.

    import java.util.ArrayList;
    import java.util.List;
    
    import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
    import org.codehaus.jackson.annotate.JsonMethod;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.ObjectMapper.DefaultTyping;
    import org.codehaus.jackson.map.ObjectWriter;
    
    public class JacksonFoo
    {
      public static void main(String[] args) throws Exception
      {
        Baz baz = new Baz("BAZ", 42);
        Zab zab = new Zab("ZAB", true);
        List<Bar> bars = new ArrayList<Bar>();
        bars.add(baz);
        bars.add(zab);
    
        ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
        mapper.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "type");
    
        String json1 = mapper.writeValueAsString(bars);
        System.out.println(json1);
        // output:
        // [
        //   {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42},
        //   {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true}
        // ]
    
        Foo foo = new Foo(bars);
    
        String json2 = mapper.writeValueAsString(foo);
        System.out.println(json2);
        // output:
        // {
        //   "bars":
        //   [
        //     "java.util.ArrayList",
        //     [
        //       {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42},
        //       {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true}
        //     ]
        //   ]
        // }
    
        mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
        mapper.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "type");
        ObjectWriter typedWriter = mapper.writerWithType(mapper.getTypeFactory().constructCollectionType(List.class, Bar.class));
    
        String json3 = typedWriter.writeValueAsString(bars);
        System.out.println(json3);
        // output:
        // [
        //   "java.util.ArrayList",
        //   [
        //     {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42},
        //     {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true}
        //   ]
        // ]
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I want use html5's new tag to play a wav file (currently only supported

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.