2017-05-19 138 views
-1

我創建了一個模型:GSON忽略我的領域,而轉換

public class UserRequest extends DefaultRequest { 
    public String username; 
    public String password; 
    public String id; 

    public UserRequest(String username, String password) { 
     this.username = username; 
     this.password = password; 
    } 
} 

我打電話是想:

//code truncated 

      UserRequest userRequest = new UserRequest(username,password); 
      response = getRestClient().sysInitApp(userRequest).execute(); 

//code truncated 

然後打印出請求主體,而不是:

{ 
"username":"farid", 
"password":"passfarid", 
"id":null 
} 

我收到:

{ 
"username":"farid", 
"password":"passfarid" 
} 

我將不勝感激這個問題的任何幫助。

+2

這是gson的默認行爲。 https://sites.google.com/site/gson/gson-user-guide#TOC-Null-Object-Support – pvg

回答

3

GsonBuilder的javadoc ...您可以使用GsonBuilder來構建你的Gson實例,並選用具有空值序列化爲這樣:

Gson gson = new GsonBuilder() 
    .serializeNulls() 
    .create(); 
+0

@pvg,已經指出,它解決了我的問題。無論如何,接受它,因爲它以適當的方式回答我的問題;) – FARID

1

不太熟悉Gson,但我不認爲Gson會將空值寫入json文件。如果你初始化這樣的ID:

String id = ""; 

你可能會在那裏得到一個空字符串。但是你不會在.xml文件中得到一個空值。

+0

獲得''「''好了,但我沒有將該字段轉換爲json。那就是問題所在。 – FARID

+0

Gson不會將空值寫入文件。所以如果它是空的,這個字段不會被寫入。 – Zoe

+0

誰低估了,爲什麼? – Zoe

0

的如何執行輸出即使值的例子空。它將輸出空字符串(或「{}」如果一個對象)而不是null並忽略瞬變:

package unitest; 

import java.io.IOException; 
import java.lang.reflect.Field; 
import java.lang.reflect.Modifier; 

import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 
import com.google.gson.TypeAdapter; 
import com.google.gson.TypeAdapterFactory; 
import com.google.gson.reflect.TypeToken; 
import com.google.gson.stream.JsonReader; 
import com.google.gson.stream.JsonToken; 
import com.google.gson.stream.JsonWriter; 

public class TheResponse<T> { 
    private String status; 
    private String message; 
    private T data; 
    transient private String resource; 

    public static void main(String[] args) { 

     TheResponse<String> foo = new TheResponse<String>(); 
     //TheResponse<Baz> foo = new TheResponse<Baz>(); 
     foo.status = "bar"; 
     foo.data = "baz"; 

     Gson gson = new GsonBuilder().registerTypeAdapter(TheResponse.class, new GenericAdapter()).create(); 

     System.out.println(gson.toJson(foo).toString()); 
    } 

    public static class GenericAdapter extends TypeAdapter<Object> { 
     @Override 
     public void write(JsonWriter jsonWriter, Object o) throws IOException { 
      recursiveWrite(jsonWriter, o); 
     } 

     private void recursiveWrite(JsonWriter jsonWriter, Object o) throws IOException { 
      jsonWriter.beginObject(); 
      for (Field field : o.getClass().getDeclaredFields()) { 
       boolean isTransient = Modifier.isTransient(field.getModifiers()); 
       if (isTransient) { 
        continue; 
       } 
       Object fieldValue = null; 
       try { 
        field.setAccessible(true); 
        fieldValue = field.get(o); 
       } catch (IllegalArgumentException e) { 
        e.printStackTrace(); 
       } catch (IllegalAccessException e) { 
        e.printStackTrace(); 
       } 
       jsonWriter.name(field.getName()); 
       if (fieldValue != null && fieldValue.getClass() != String.class) { 
        recursiveWrite(jsonWriter, fieldValue); 
        continue; 
       } 
       if (fieldValue == null) { 
        if (field.getType() == String.class) 
         jsonWriter.value(""); 
        else { 
         jsonWriter.jsonValue("{}"); 
        } 
       } else { 
        jsonWriter.value(fieldValue.toString()); 
       } 
      } 
      jsonWriter.endObject(); 
     } 

     @Override 
     public Object read(JsonReader jsonReader) throws IOException { 
      // todo 
      return null; 
     } 

    } 
}