2016-02-18 12 views
7

Tôi muốn tuần tự hóa null cho một trường hoặc lớp cụ thể.Gson tuần tự hóa null cho lớp hoặc trường cụ thể

Trong GSON, tùy chọn serializeNulls() áp dụng cho toàn bộ JSON.

Ví dụ:

class MainClass { 
    public String id; 
    public String name; 
    public Test test; 
} 

class Test { 
    public String name; 
    public String value;  
} 

MainClass mainClass = new MainClass(); 
mainClass.id = "101" 
// mainClass has no name. 
Test test = new Test(); 
test.name = "testName"; 
test.value = null; 
mainClass.test = test;  

Tạo JSON sử dụng GSON:

GsonBuilder builder = new GsonBuilder().serializeNulls(); 
Gson gson = builder.create(); 
System.out.println(gson.toJson(mainClass)); 

ouput hiện tại:

{ 
    "id": "101", 
    "name": null, 
    "test": { 
     "name": "testName", 
     "value": null 
    } 
} 

đầu ra mong muốn:

{ 
    "id": "101", 
    "test": { 
     "name": "testName", 
     "value": null 
    } 
} 

Làm thế nào để đạt được kết quả mong muốn?

giải pháp ưu tiên sẽ có các thuộc tính sau:

  • Do KHÔNG null serialize theo mặc định,
  • null Serialize cho các trường có một chú thích cụ thể.
+0

@DatoMumladze i đã cập nhật câu hỏi của tôi – Martin

+0

Tôi không thể tìm thấy tính năng này trong Gson. Đây là một số [link] thú vị (https://mindfirejavaexperts.wordpress.com/2014/03/14/how-to-use-gson-library/) Hoặc bạn có thể sử dụng Jackson để tuần tự hóa đối tượng thành json và loại trừ các giá trị null cho các trường cụ thể sử dụng chú thích này '@JsonSerialize (include = JsonSerialize.Inclusion.NON_NULL)' –

+0

Có thể trùng lặp của [Loại trừ các trường nhất định khỏi Tuần tự hóa dựa trên giá trị trong GSON] (http://stackoverflow.com/questions/13120354/excluding-certain -trên-serial-based-based-on-value-in-gson) – tima

Trả lời

0

Tôi có giao diện để kiểm tra khi đối tượng cần được tuần tự như null:

public interface JsonNullable { 
    boolean isJsonNull(); 
} 

Và TypeAdapter tương ứng (hỗ trợ viết chỉ)

public class JsonNullableAdapter extends TypeAdapter<JsonNullable> { 

    final TypeAdapter<JsonElement> elementAdapter = new Gson().getAdapter(JsonElement.class); 
    final TypeAdapter<Object> objectAdapter = new Gson().getAdapter(Object.class); 

    @Override 
    public void write(JsonWriter out, JsonNullable value) throws IOException { 
    if (value == null || value.isJsonNull()) { 
     //if the writer was not allowed to write null values 
     //do it only for this field 
     if (!out.getSerializeNulls()) { 
     out.setSerializeNulls(true); 
     out.nullValue(); 
     out.setSerializeNulls(false); 
     } else { 
     out.nullValue(); 
     } 
    } else { 
     JsonElement tree = objectAdapter.toJsonTree(value); 
     elementAdapter.write(out, tree); 
    } 
    } 

    @Override 
    public JsonNullable read(JsonReader in) throws IOException { 
    return null; 
    } 
} 

Sử dụng nó như sau:

public class Foo implements JsonNullable { 
    @Override 
    public boolean isJsonNull() { 
    // You decide 
    } 
} 

Trong lớp mà giá trị Foo phải là s erialized là null. Lưu ý rằng giá trị foo chính nó không được null, nếu không chú thích bộ điều hợp tùy chỉnh sẽ bị bỏ qua.

public class Bar { 
    @JsonAdapter(JsonNullableAdapter.class) 
    public Foo foo = new Foo(); 
} 
Các vấn đề liên quan