Suppose I have json tree already read.
Is it possible to deserialize from it (without converting back to string)?
public class TryDeserializeNode {
   public static class MyClass {
      private int x = 11, y = 12;
      public int getX() {
         return x;
      }
      public void setX(int x) {
         this.x = x;
      }
      public int getY() {
         return y;
      }
      public void setY(int y) {
         this.y = y;
      }
   }
   public static void main(String[] args) throws IOException {
      ObjectMapper mapper = new ObjectMapper();
      MyClass myClass = new MyClass();
      String string = mapper.writeValueAsString(myClass);
      JsonNode tree = mapper.readTree(string);
      // how to deserialize from tree directly?
      // MyClass myclass2 = mapper.readValue(tree.toString(), MyClass.class);
      MyClass myclass2 = mapper.readValue(tree, MyClass.class);
   }
}
                You could simply use treeToValue:
MyClass myclass2 = mapper.treeToValue(tree, MyClass.class);
where mapper is your Jackson mapper and tree is your JsonNode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With