Immutable/polymorphic POJO <-> JSON serialization with Jackson

后端 未结 3 1841
南方客
南方客 2021-02-08 06:00

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

相关标签:
3条回答
  • 2021-02-08 06:22

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

    Edit: 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 {}
    
    0 讨论(0)
  • 2021-02-08 06:30

    Have a look at Genson library some of its key features are adressing your exact problem: polymorphism, not requiring annotations and most important immutable pojos. Everything works in your example with 0 annotations or heavy conf.

    Genson genson = new Genson.Builder().setWithClassMetadata(true)
                                .setWithDebugInfoPropertyNameResolver(true)
                                .create();
    
    String jsonCircle = genson.serialize(circle);
    String jsonRectangle = genson.serialize(rectangle);
    
    System.out.println(jsonCircle); // {"@class":"your.package.Circle","radius":123}
    System.out.println(jsonRectangle); // {"@class":"your.package.Rectangle","w":20,"h":30}
    
    // Throws nothing :)
    Shape newCircle = genson.deserialize(jsonCircle, Shape.class);
    Shape newRectangle = genson.deserialize(jsonRectangle, Shape.class);
    

    Genson gives you also the ability to use aliases (used instead classes names).

    new Genson.Builder().addAlias("shape", Shape.class)
                    .addAlias("circle", Circle.class)
                    .create();
    
    0 讨论(0)
  • 2021-02-08 06:42

    Rectangle has two parameters, and the FAQ says:

    Deserializing simple types

    If I want to deserialize simple JSON values (Strings, integer / decimal numbers) into types other than supported by default, do I need to write a custom deserializer?

    Not necessarily. If the class to deserialize into has one of:

    • Single-argument constructor with matching type (String, int/double), or
    • Single-argument static method with name "valueOf()", and matching argument type

    Jackson will use such method, passing in matching JSON value as argument.

    I'm afraid you have to write your own deserializer as show in the Jackson documentation:

    ObjectMapper mapper = new ObjectMapper();
    SimpleModule testModule =
       new SimpleModule("MyModule", new Version(1, 0, 0, null))
          .addDeserializer( MyType.class, new MyTypeDeserializer());
    mapper.registerModule( testModule );
    
    0 讨论(0)
提交回复
热议问题