0

currently I'm doing this to convert JsonNode to a POJO:

try {
    MyClass obj = mapper.treeToValue(jsonData, MyClass.class)));
    /* consume obj */
} catch (JsonProcessingException ex) {
    return false;
}

But sometimes I don't want to get a new instance of MyClass. I have have populated it before and now I just want to set new values for some fields and keep old values for unchanged fields. What I want is something like this: mapper.readJsonNodeToPOJO(jsonData, obj))); Is it possible to do this?

Majid Azimi
  • 5,251
  • 13
  • 60
  • 106

2 Answers2

1

Use the ObjectMapper.readerForUpdating method. Similar question has been asked here and here

Community
  • 1
  • 1
Alexey Gavrilov
  • 10,115
  • 2
  • 34
  • 45
0

You can do an inner custom object:

public class MyClass{
  MyNode node:
  ....
}

public class MyNode{
   .....
}

This way you can do:

MyClass obj = mapper.treeToValue(jsonData, MyClass.class)));

and:

obj.setNode(mapper.treeToValue(jsonSubData, MyNode.class))
inigoD
  • 1,653
  • 13
  • 26