Skip to content

How to serialize generic/polymorphic objects starting with 2.12.0-rc2? #4075

Answered by JooHyukKim
piakbt asked this question in Q&A
Discussion options

You must be logged in to vote

@piakbt Idk what changed between the two 2.12.0-rc versions though.

Can you try below Java code? Below works in latest jackson-databind 2.12 branch. To point out the difference...

  • Using .writerFor(new TypeReference<JustT<Base>>(){}), and
  • And @JsonValue annotated method returns T. Check out @JsonValue documentation
    public void testSer() throws Exception {
        // provide typeReference
        ObjectWriter writer = new ObjectMapper()
                .writerFor(new TypeReference<JustT<Base>>(){});
        
        String output = writer.writeValueAsString(new JustT<Base>(new MedianMethod()));
        
        assertEquals("{\"type\":\"Median\"}", output);
    }

    @JsonTypeInfo(use

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
6 replies
@JooHyukKim
Comment options

@JooHyukKim
Comment options

@piakbt
Comment options

@piakbt
Comment options

@JooHyukKim
Comment options

Answer selected by piakbt
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants