Niezmienna / polimorficzna serializacja POJO <-> JSON z Jacksonem

Próbuję serializować niezmienny POJO do iz JSON, używając Jackson 2.1.4, bez konieczności pisania niestandardowego serializatora i przy jak najmniejszej liczbie adnotacji. Lubię też unikać dodawania niepotrzebnych getterów lub domyślnych konstruktorów, aby zaspokoić bibliotekę Jacksona.

Teraz utknąłem na wyjątku:

JsonMappingException: Nie znaleziono odpowiedniego konstruktora dla typu [typ prosty, klasa Circle]: nie można utworzyć instancji z obiektu JSON (trzeba dodać / włączyć informacje o typie?)

Kod:

public abstract class Shape {}


public class Circle extends Shape {
  public final int radius; // Immutable - no getter needed

  public Circle(int radius) {
    this.radius = radius;
  }
}


public class Rectangle extends Shape {
  public final int w; // Immutable - no getter needed
  public final int h; // Immutable - no getter needed

  public Rectangle(int w, int h) {
    this.w = w;
    this.h = h;
  }
}

Kod testowy:

ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); // Adds type info

Shape circle = new Circle(10);
Shape rectangle = new Rectangle(20, 30);

String jsonCircle = mapper.writeValueAsString(circle);
String jsonRectangle = mapper.writeValueAsString(rectangle);

System.out.println(jsonCircle); // {"@class":"Circle","radius":123}
System.out.println(jsonRectangle); // {"@class":"Rectangle","w":20,"h":30}

// Throws:
//  JsonMappingException: No suitable constructor found.
//  Can not instantiate from JSON object (need to add/enable type information?)
Shape newCircle = mapper.readValue(jsonCircle, Shape.class);
Shape newRectangle = mapper.readValue(jsonRectangle, Shape.class);

System.out.println("newCircle = " + newCircle);
System.out.println("newRectangle = " + newRectangle);

Każda pomoc jest bardzo mile widziana, dzięki!

questionAnswers(3)

yourAnswerToTheQuestion