-1

I got a deserialization problem: Trying to convert class which extends parent class. Please find my class below,

public class Base { Parent parent; public Parent getParent() { return parent; } public void setParent(Parent parent) { this.parent = parent; } } ----------------- public class Parent { String name; String city; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } } ----------------- public class Child extends Parent { String field; public String getField() { return field; } public void setField(String field) { this.field = field; } } 

Main method:

public class TestDeserial { public static void main(String[] args) { Child c = new Child(); c.setName("test1"); c.setCity("Mumbai"); c.setField("allow"); Base b = new Base(); b.setParent(c); // Convert object to json File file = new File("C:\\temp\\c.json"); try { ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(file, b); } catch (IOException e) { e.printStackTrace(); } // Convert json to object Base baseResult = null; try (InputStream in = new FileInputStream("C:\\temp\\c.json")) { ObjectMapper objMapper = new ObjectMapper(); baseResult = objMapper.readValue(in, Base.class); } catch (Exception e1) { e1.printStackTrace(); } } } 

Error: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "field" (class com.mapper.example.Parent), not marked as ignorable (2 known properties: "name", "city"]) at [Source: (FileInputStream); line: 1, column: 52] (through reference chain: com.mapper.example.Base["parent"]->com.mapper.example.Parent["field"])

I don't want to add any annotation in class file.

3
  • stackoverflow.com/questions/23469784/… Commented Apr 4, 2023 at 9:03
  • Thanks for your reply. I tried the accepted answer, but with that solution we are missing the Child class member "field" in baseResult object. Commented Apr 4, 2023 at 9:13
  • If you don't want to add any annotation you can check the presence or not of the "field" property. If you want I can write a possible solution without annotations, but this could be resolved automatically with jackson annotations for polymorphism. Commented Apr 4, 2023 at 15:57

1 Answer 1

1

You need to tell Jackson to use deduction-based inference for determing the appropriate sub-class. Jackson will inspect the fields in the JSON and compare to the fields in the sub-classes.

If you don't want to add annotations to the original classes then you can use mix-ins, e.g.:

@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION) @JsonSubTypes({ @JsonSubTypes.Type(Child.class) }) public abstract class ParentMixin {} 

and then just register it with your ObjectMapper (the one used for deserialisation):

ObjectMapper mapper = new ObjectMapper(); mapper.addMixIn(Parent.class, ParentMixin.class); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.