2011-12-07 38 views
6

Tôi đang thử nghiệm với việc sắp xếp/tuần tự hóa Jackson. Ví dụ: tôi có lớp học như vậy:Việc sắp xếp danh sách phức tạp của Jackson

class Base{ 
    String baseId; 
} 

Và tôi muốn sắp xếp theo thứ tự Danh sách objs; Để làm điều đó với jackson, tôi cần phải xác định các yếu tố của một danh sách các loại thực, do việc xóa loại java. Mã này sẽ làm việc:

List<Base> data = getData(); 
return new ObjectMapper().writerWithType(TypeFactory.collectionType(List.class, Base.class)).writeValueAsString(data); 

Bây giờ, tôi muốn serialize lớp phức tạp hơn:

class Result{ 
    List<Base> data; 
} 

Làm thế nào tôi nên nói với Jackson để serialize đúng lớp này?

+1

Lưu ý: Không nên sử dụng TypeFactory trong mã ví dụ trong câu hỏi gốc. Thay vào đó, hãy sử dụng mapper.getTypeFactory(). ConstructCollectionType(). –

+0

Tôi không đoán cách chỉ định Danh sách làm đối số constructCollectionType. Đó là lý do tại sao tôi sử dụng collectionType ở đó. – tmp120210

Trả lời

14

Chỉ

new ObjectMapper().writeValueAsString(myResult); 

Các loại danh sách sẽ không bị mất do chô bôi loại trong cùng một cách nó sẽ là trong ví dụ đầu tiên.


Lưu ý rằng để tuần tự hóa vani danh sách hoặc danh sách chung, không cần phải chỉ định loại thành phần danh sách, như được minh họa trong ví dụ trong câu hỏi gốc. Cả ba ví dụ sau đây đều đại diện cho List<Bar> với cùng một JSON chính xác.

import java.util.ArrayList; 
import java.util.List; 

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; 
import org.codehaus.jackson.annotate.JsonMethod; 
import org.codehaus.jackson.map.ObjectMapper; 
import org.codehaus.jackson.map.ObjectWriter; 

public class JacksonFoo 
{ 
    public static void main(String[] args) throws Exception 
    { 
    Baz baz = new Baz("BAZ", 42); 
    Zab zab = new Zab("ZAB", true); 
    List<Bar> bars = new ArrayList<Bar>(); 
    bars.add(baz); 
    bars.add(zab); 

    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY); 

    String json1 = mapper.writeValueAsString(bars); 
    System.out.println(json1); 
    // output: 
    // [{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}] 

    Foo foo = new Foo(bars); 

    String json2 = mapper.writeValueAsString(foo); 
    System.out.println(json2); 
    // output: 
    // {"bars":[{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}]} 

    mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY); 
    ObjectWriter typedWriter = mapper.writerWithType(mapper.getTypeFactory().constructCollectionType(List.class, Bar.class)); 

    String json3 = typedWriter.writeValueAsString(bars); 
    System.out.println(json3); 
    // output: 
    // [{"name":"BAZ","size":42},{"name":"ZAB","hungry":true}] 
    } 
} 

class Foo 
{ 
    List<Bar> bars; 
    Foo(List<Bar> b) {bars = b;} 
} 

abstract class Bar 
{ 
    String name; 
    Bar(String n) {name = n;} 
} 

class Baz extends Bar 
{ 
    int size; 
    Baz(String n, int s) {super(n); size = s;} 
} 

class Zab extends Bar 
{ 
    boolean hungry; 
    Zab(String n, boolean h) {super(n); hungry = h;} 
} 

Một nhà văn được nhập có ích khi tuần tự hóa với thông tin loại bổ sung. Lưu ý cách các kết quả đầu ra json1json3 dưới đây khác nhau.

import java.util.ArrayList; 
import java.util.List; 

import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; 
import org.codehaus.jackson.annotate.JsonMethod; 
import org.codehaus.jackson.map.ObjectMapper; 
import org.codehaus.jackson.map.ObjectMapper.DefaultTyping; 
import org.codehaus.jackson.map.ObjectWriter; 

public class JacksonFoo 
{ 
    public static void main(String[] args) throws Exception 
    { 
    Baz baz = new Baz("BAZ", 42); 
    Zab zab = new Zab("ZAB", true); 
    List<Bar> bars = new ArrayList<Bar>(); 
    bars.add(baz); 
    bars.add(zab); 

    ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY); 
    mapper.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "type"); 

    String json1 = mapper.writeValueAsString(bars); 
    System.out.println(json1); 
    // output: 
    // [ 
    // {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42}, 
    // {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true} 
    // ] 

    Foo foo = new Foo(bars); 

    String json2 = mapper.writeValueAsString(foo); 
    System.out.println(json2); 
    // output: 
    // { 
    // "bars": 
    // [ 
    //  "java.util.ArrayList", 
    //  [ 
    //  {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42}, 
    //  {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true} 
    //  ] 
    // ] 
    // } 

    mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY); 
    mapper.enableDefaultTypingAsProperty(DefaultTyping.OBJECT_AND_NON_CONCRETE, "type"); 
    ObjectWriter typedWriter = mapper.writerWithType(mapper.getTypeFactory().constructCollectionType(List.class, Bar.class)); 

    String json3 = typedWriter.writeValueAsString(bars); 
    System.out.println(json3); 
    // output: 
    // [ 
    // "java.util.ArrayList", 
    // [ 
    //  {"type":"com.stackoverflow.q8416904.Baz","name":"BAZ","size":42}, 
    //  {"type":"com.stackoverflow.q8416904.Zab","name":"ZAB","hungry":true} 
    // ] 
    // ] 
    } 
} 
+0

Ah oh, tôi đã rất gần với giải pháp) Một cái gì đó đã cho tôi một NPE khi tôi đã thử nó lần đầu tiên, và tôi nghĩ rằng nó sẽ không hoạt động. Bây giờ tôi đã thử lại và mọi thứ hoạt động. Cảm ơn! – tmp120210

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