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

  • Home
  • SEARCH
  • 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 6091207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:18:04+00:00 2026-05-23T12:18:04+00:00

I’ve some classes A, B, C they all inherit from class BaseClass. I’ve a

  • 0

I’ve some classes A, B, C they all inherit from class BaseClass.

I’ve a String json that contains the json representation of the A, B, C or BaseClass.

I want to have some way to deserialize this String to the BaseClass (polymorphic deserialization). Something like this

BaseClass base = ObjectMapper.readValue(jsonString, BaseClass.class);

jsonString could be Json String representation of any of A, B, C, or BaseClass.

  • 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-23T12:18:05+00:00Added an answer on May 23, 2026 at 12:18 pm

    It’s not clear what problem the original poster is having. I’m guessing that it’s one of two things:

    1. Deserialization problems with unbound JSON elements, because the JSON contains elements for which there is nothing in the Java to bind to; or

    2. Want to implement polymorphic deserialization.

    Here’s a solution to the first problem.

    import static org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES;
    
    import org.codehaus.jackson.map.ObjectMapper;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        BaseClass base = new BaseClass();
        A a = new A();
        B b = new B();
        C c = new C();
    
        ObjectMapper mapper = new ObjectMapper();
    
        String baseJson = mapper.writeValueAsString(base);
        System.out.println(baseJson); // {"baseName":"base name"}
        String aJson = mapper.writeValueAsString(a);
        System.out.println(aJson); // {"baseName":"base name","aName":"a name"}
        String bJson = mapper.writeValueAsString(b);
        System.out.println(bJson); // {"baseName":"base name","bName":"b name"}
        String cJson = mapper.writeValueAsString(c);
        System.out.println(cJson); // {"baseName":"base name","cName":"c name"}
    
        BaseClass baseCopy = mapper.readValue(baseJson, BaseClass.class);
        System.out.println(baseCopy); // baseName: base name
    
        // BaseClass aCopy = mapper.readValue(aJson, BaseClass.class);
        // throws UnrecognizedPropertyException: 
        // Unrecognized field "aName", not marked as ignorable
        // because the JSON contains elements for which no Java field
        // to bind to was provided.
    
        // Need to let Jackson know that not all JSON elements must be bound.
        // To resolve this, the class can be annotated with 
        // @JsonIgnoreProperties(ignoreUnknown=true) or the ObjectMapper can be
        // directly configured to not FAIL_ON_UNKNOWN_PROPERTIES
        mapper = new ObjectMapper();
        mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    
        BaseClass aCopy = mapper.readValue(aJson, BaseClass.class);
        System.out.println(aCopy); // baseName: base name
        BaseClass bCopy = mapper.readValue(bJson, BaseClass.class);
        System.out.println(bCopy); // baseName: base name
        BaseClass cCopy = mapper.readValue(cJson, BaseClass.class);
        System.out.println(cCopy); // baseName: base name
      }
    }
    
    class BaseClass
    {
      public String baseName = "base name";
      @Override public String toString() {return "baseName: " + baseName;}
    }
    
    class A extends BaseClass
    {
      public String aName = "a name";
      @Override public String toString() {return super.toString() + ", aName: " + aName;}
    }
    
    class B extends BaseClass
    {
      public String bName = "b name";
      @Override public String toString() {return super.toString() + ", bName: " + bName;}
    }
    
    class C extends BaseClass
    {
      public String cName = "c name";
      @Override public String toString() {return super.toString() + ", cName: " + cName;}
    }
    

    Here’s a solution to the second problem.

    import org.codehaus.jackson.annotate.JsonSubTypes;
    import org.codehaus.jackson.annotate.JsonSubTypes.Type;
    import org.codehaus.jackson.annotate.JsonTypeInfo;
    import org.codehaus.jackson.map.ObjectMapper;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        BaseClass base = new BaseClass();
        A a = new A();
        B b = new B();
        C c = new C();
    
        ObjectMapper mapper = new ObjectMapper();
    
        String baseJson = mapper.writeValueAsString(base);
        System.out.println(baseJson); // {"type":"BaseClass","baseName":"base name"}
        String aJson = mapper.writeValueAsString(a);
        System.out.println(aJson); // {"type":"a","baseName":"base name","aName":"a name"}
        String bJson = mapper.writeValueAsString(b);
        System.out.println(bJson); // {"type":"b","baseName":"base name","bName":"b name"}
        String cJson = mapper.writeValueAsString(c);
        System.out.println(cJson); // {"type":"c","baseName":"base name","cName":"c name"}
    
        BaseClass baseCopy = mapper.readValue(baseJson, BaseClass.class);
        System.out.println(baseCopy); // baseName: base name
        BaseClass aCopy = mapper.readValue(aJson, BaseClass.class);
        System.out.println(aCopy); // baseName: base name, aName: a name
        BaseClass bCopy = mapper.readValue(bJson, BaseClass.class);
        System.out.println(bCopy); // baseName: base name, bName: b name
        BaseClass cCopy = mapper.readValue(cJson, BaseClass.class);
        System.out.println(cCopy); // baseName: base name, cName: c name
      }
    }
    
    @JsonTypeInfo(  
        use = JsonTypeInfo.Id.NAME,  
        include = JsonTypeInfo.As.PROPERTY,  
        property = "type")  
    @JsonSubTypes({  
        @Type(value = A.class, name = "a"),  
        @Type(value = B.class, name = "b"),  
        @Type(value = C.class, name = "c") }) 
    class BaseClass
    {
      public String baseName = "base name";
      @Override public String toString() {return "baseName: " + baseName;}
    }
    
    class A extends BaseClass
    {
      public String aName = "a name";
      @Override public String toString() {return super.toString() + ", aName: " + aName;}
    }
    
    class B extends BaseClass
    {
      public String bName = "b name";
      @Override public String toString() {return super.toString() + ", bName: " + bName;}
    }
    
    class C extends BaseClass
    {
      public String cName = "c name";
      @Override public String toString() {return super.toString() + ", cName: " + cName;}
    }
    

    If instead, the goal is to deserialize to a subclass type without a JSON element specifically dedicated to indicate what the subclass type is, then that is also possible, so long as something in the JSON can be used to decide what the subclass type should be. I posted an example of this approach at http://programmerbruce.blogspot.com/2011/05/deserialize-json-with-jackson-into.html.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.