2013-08-19 33 views
15

Theo dõi về câu hỏi này: Question hereJackson deserialize JsonIdentityReference (alwaysAsId = true)

@JsonIdentityReference(alwaysAsId = true)@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class) hoạt động tuyệt vời từ cuối serialization, nhưng không quá tốt khi nói đến thời gian để deserialize vì nó không thể giải quyết Tham chiếu ID đối tượng.

Có cách nào để có được điều này để deserialize? Viết một deserializer tùy chỉnh có vẻ như quá mức cần thiết.

Trả lời

8

Thay vì một deserializer tùy chỉnh, bạn có thể sử dụng một deserializer setter đơn giản:

public class Container { 
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 
    @JsonIdentityReference(alwaysAsId = true) 
    private Foo foo; 

    public Foo getFoo() { 
     return foo; 
    } 
    public Container setFoo(Foo foo) { 
     this.foo = foo; 
     return this; 
    } 
    @JsonProperty("foo") 
    public void setFoo(String id) { 
     foo = new Foo().setId(id); 
    } 
} 

chuỗi Ví dụ về {"foo":"id1"} được đăng đúng với phương pháp này trong Jackson 2.5.2

+0

này không hoạt động nếu Foo có tính hơn chỉ là id và, do đó, các cá thể hiện có phải được liên kết. – koppor

Các vấn đề liên quan