不可变/多晶型POJO - 。与杰克逊的JSON序列化 [英] Immutable/polymorphic POJO <-> JSON serialization with Jackson

查看:113
本文介绍了不可变/多晶型POJO - 。与杰克逊的JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jackson 2.1.4将不可变POJO与JSON序列化,而不必编写自定义序列化程序并尽可能少注释。我还想避免添加不必要的getter或默认构造函数只是为了满足Jackson库。

I'm trying to serialize a immutable POJO to and from JSON, using Jackson 2.1.4, without having to write a custom serializer and with as few annotations as possible. I also like to avoid having to add unnecessary getters or default constructors just to satisfy the Jackson library.

我现在停留在例外:

JsonMappingException:找不到类型[simple type,class Circle]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息?)

JsonMappingException: No suitable constructor found for type [simple type, class Circle]: can not instantiate from JSON object (need to add/enable type information?)

代码:

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;
  }
}

测试代码:

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);

非常感谢任何帮助,谢谢!

Any help is greatly appreciated, thanks!

推荐答案

您可以(根据API)使用 @ JsonCreator 和参数 @ JsonProperty

You could (according to the API) annotate the constructor with @JsonCreator and the parameters with @JsonProperty.

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

    @JsonCreator
    public Circle(@JsonProperty("radius") 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

    @JsonCreator        
    public Rectangle(@JsonProperty("w") int w, @JsonProperty("h") int h) {
        this.w = w;
        this.h = h;
    }
}

编辑:也许你必须用Shape注释Shape类 @JsonSubTypes so可以确定Shape的具体子类。

Maybe you have to annotate the Shape class with @JsonSubTypes so that the concrete subclass of Shape could be determined.

@JsonSubTypes({@JsonSubTypes.Type(Circle.class), @JsonSubTypes.Type(Rectangle.class)})
public abstract class Shape {}

这篇关于不可变/多晶型POJO - 。与杰克逊的JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆