どのようにGSONで1つの特定のヌルフィールドを返すには?

Shell_Leko:

私は単純なオブジェクトを持っている場合は、次のように:

String name;
String email;
int age;
boolean isDeveloper;

その後のは、JSONオブジェクトが値を持って受けましょう:

{"name":null,"email":null,"age":26,"isDeveloper":true}

デフォルトとしてGSONを使用して、このJSONをデシリアライズするときに私が持っています:

{"age":26,"isDeveloper":true}

私は追加したいので、しかし、不足している電子メールのフィールドは自分のアプリケーション上で、その後の故障の原因となります

email = null;

JSONにシリアライズバックとNULLとしてのみ、このフィールドの値を持っています。また、任意の非ヌルフィールドを無視していません。

他のNULL値が得られたJSONに追加されるべきではありません。

私はその後としてnull値を許可するGSONでシリアライズデフォルトGSONビルダーをデシリアライズしようとしました:

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

問題は次のとおりです。これは、オブジェクトクラスからすべてのNULL /空の値を見て、それらすべてを設定します

{"name":null,"email":null,"age":26,"isDeveloper":true}

どのように私は、他の非ヌル値を無視することなく、ヌルとしてのみ、このフィールドを含むバックJSONにシリアル化後、電子メールのプロパティはnullを設定することができますか?

私はgson v2.2.4を使用しています

ここでは例のコードは次のとおりです。

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class App
{
    private class UserSimple {
        public String name;
        public String email;
        public int age;
        public boolean isDeveloper;


        UserSimple(String name, String email, int age, boolean isDeveloper) {
            this.name = name;
            this.email = email;
            this.age = age;
            this.isDeveloper = isDeveloper;
        }

    }

    public static void main( String[] args )
    {
        String userJson = "{'age':26,'email':'[email protected]','isDeveloper':true,'name':null}";
        Gson gson = new GsonBuilder()
                .setPrettyPrinting()
                .disableHtmlEscaping()
                .create();


        Gson gsonBuilder = new GsonBuilder().serializeNulls().create();

        UserSimple deserializedObj = gson.fromJson(userJson, UserSimple.class);
        System.out.println("\n"+gson.toJson(deserializedObj));
        deserializedObj.email = null;


        String serializedObj = gsonBuilder.toJson(deserializedObj, UserSimple.class);
        System.out.println("\n"+serializedObj);


    }
}
mnj11:

あなたは手で問題を解決するために、独自のカスタムアダプタを作成することができます。

class MyCustomTypeAdapter extends TypeAdapter<UserSimple> {
    @Override
    public void write(JsonWriter writer, UserSimple userSimple) throws IOException {
        writer.beginObject();
        if(userSimple.getName() != null){
            writer.name("name");
            writer.value(userSimple.getName());
        }

        // you want to include email even if it's null
        writer.name("email");
        writer.value(userSimple.getEmail());

        if(userSimple.getAge() != null){
            writer.name("age");
            writer.value(userSimple.getAge());
        }
        if(userSimple.getDeveloper() != null){
            writer.name("isDeveloper");
            writer.value(userSimple.getDeveloper());
        }
        writer.endObject();
    }

    public UserSimple read(JsonReader reader) throws IOException {
    // you could create your own
        return null;
    }
} 

入力:

String userJson = "{'age':null,'email':null,'isDeveloper':true,'name':'somename'}";

出力:

serializedObj :{"name":"somename","email":null,"isDeveloper":true}

入力:

String userJson = "{'age':20,'email':null,'isDeveloper':true,'name':'somename'}";

出力:

serializedObj :{"name":"somename","email":null,"age":20,"isDeveloper":true}

見てhttps://google.github.io/gson/apidocs/com/google/gson/TypeAdapter.html

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=203064&siteId=1