1

I am receiving from the API this JSON:

    "link": [],
    "firstRecord": 1,
    "item": [
        {
            "Customer": {
                "id": "1058207",
                "firstName": "foo",
                "lastName": "foo2",
                "nestedObj1": {
                    "id": "40008"
                },
                "nestedObj2": {
                    "link": [],
                    "linkfoo": "lala",
                    "item": [
                             {
                              "id": "266614",
                              "label": "NESTED_OBJ_2"
                            }
                           ]
                  }
              ]
           }

My Deserializer function

    @Override
    public CustomerView deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {

        //tried this too
        TreeNode treeNode = p.getCodec().readTree(p);

        // also this
        JsonNode node = p.getCodec().readTree(p);

        JsonNode simpleNode = new ObjectMapper().readTree(p);

        // use for each field that is not String
        ObjectMapper mapper = new ObjectMapper();

        Customer customer = new Customer();

        customer.setFirstName(simpleNode.get("Customer").get("firstName").textValue()); 

        NestedObj2[] arrayObj2 = mapper.readValue(
                        simpleNode.get("Customer").get("nestedObj2").get("item").toString(), 
                        NestedObj2[].class);

        customer.setArrayObj2(arrayObj2);
}

Class NestedObj2 has all the fields as in the JSON, "item" array is separate object as a field.

The problem is, that neither JsonNode nor TreeNode, doesn't see field "nestedObj2", but the rest of the fields are inside them while deserializing -> checked while debugging.

Do I miss something in configuration, or should I use other object to deserialize?

Thanks!

EDIT

In the end I've used DTO as @Mehrdad HosseinNejad advised. As I'm receiving this JSON by RestTemplate.exchange(), I had to configure RestTemplate with MappingJacksonHttpMessageConverter like here https://stackoverflow.com/a/9381832/12677470

jogobella
  • 13
  • 4

1 Answers1

1

Use of DTO classes may be a better idea, there is no need to create custom deserializer

I coded an example nested DTO as follows

Create DTO class for Customer

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Customer {

private Long id;
private String firstName;
private String lastName;
private NestedObj1 nestedObj1;
private NestedObj2 nestedObj2;

//getter and setter

}

Create DTO class for NestedObj1

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class NestedObj1 {

private Long id;

//getter and setter

}

Create DTO class for NestedObj2

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class NestedObj2 {

private List<String> link;
private String linkFoo;
private List<Item> item;

//getter and setter     

}

Create DTO class for Item

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Item {

private Long id;
private String label;

//getter and setter

}

After creating these DTO's you can simply using ObjectMapper class convert your JSON to Java Object

Customer customer= new ObjectMapper().readValue(jsonFile, Customer.class);

For more options like ignore some property,... you can use the below link:

Jackson Annotation Examples

More information in Deserialization:

Getting Started with Deserialization in Jackson

  • thanks for the answer! the problem is, that this json has much much more fields, that I don't want to put in my POJOs, that's why I've used custom deserializer. Anyway, I've got DTO as you wrote above, but still, the deserializer itself (JsonNode) doesn't have field 'nestedObj2' in its map. I am searching for solution, what it's happening – jogobella Jan 13 '20 at 22:15
  • If you don't want to put any JSON property into POJO, you can put @JsonIgnore on that property to ignore it!. Or you can delete that property from POJO(like if you don't want id conversion, it's better to delete it from Item class) – Mehrdad HosseinNejad Yami Jan 13 '20 at 22:39
  • https://stackoverflow.com/questions/38077728/what-is-the-difference-between-a-property-with-jsonignore-and-one-with-no-annot – Mehrdad HosseinNejad Yami Jan 13 '20 at 22:44
  • 1
    In the end I've used proper DTO without deserializer. Thank you! – jogobella Jan 14 '20 at 17:43