web-dev-qa-db-ja.com

異なるオブジェクトを持つネストされたjson用のレトロフィットgsonコンバーター

私は次のようなJSON構造を持っています-

{
    "status": true,
    "message": "Registration Complete.",
    "data": {
        "user": {
            "username": "user88",
            "email": "[email protected]",
            "created_on": "1426171225",
            "last_login": null,
            "active": "1",
            "first_name": "User",
            "last_name": "",
            "company": null,
            "phone": null,
            "sign_up_mode": "GOOGLE_PLUS"
        }
    }
}

上記の形式が一般的です。 datauserproductなどのさまざまなタイプの情報を保持できるのはinvoiceキーだけです。

statusmessage、およびdataキーをすべてのREST応答で同じに保ちたい。 datastatusに従って扱われ、messageがユーザーに表示されます。

したがって、基本的に、上記の形式はすべてのAPIで必要です。 dataキー内の情報のみが毎回異なります。

そして、私は次のクラスをセットアップし、それをgsonコンバーターとしてセットアップしましたMyResponse.Java

public class MyResponse<T> implements Serializable{
    private boolean status ;
    private String message ;
    private T data;

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

Deserializer.Java

class Deserializer<T> implements JsonDeserializer<T>{
    @Override
    public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException{
        JsonElement content = je.getAsJsonObject();

        // Deserialize it. You use a new instance of Gson to avoid infinite recursion to this deserializer
        return new Gson().fromJson(content, type);

    }
}

そしてそれを次のように使用しました-

GsonBuilder  gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); 
gsonBuilder.registerTypeAdapter(MyResponse.class, new Deserializer<MyResponse>());
...... ..... ....

restBuilder.setConverter(new GsonConverter(gsonBuilder.create()));

サービスインターフェースは次のとおりです-

@POST("/register")
public void test1(@Body MeUser meUser, Callback<MyResponse<MeUser>> apiResponseCallback);


@POST("/other")
public void test2(Callback<MyResponse<Product>> apiResponseCallback);

問題

コールバック内からstatusフィールドとmessageフィールドにアクセスできます。ただし、dataキー内の情報は解析されず、MeUserProductのようなモデルは常に空として返されます。

Json構造を上記のコードに変更すると、完全に機能します-

{
        "status": true,
        "message": "Registration Complete.",
        "data": {                
                "username": "user88",
                "email": "[email protected]",
                "created_on": "1426171225",
                "last_login": null,
                "active": "1",
                "first_name": "User",
                "last_name": "",
                "company": null,
                "phone": null,
                "sign_up_mode": "GOOGLE_PLUS"
        }
    }

dataオブジェクト内で個別のキーを指定して正常に解析するにはどうすればよいですか?

16
Gimali

Jsonで何かを変更することを提案できる場合は、データのタイプを定義する1つの新しいフィールドを追加する必要があるため、jsonは次のようになります。

{
   "status": true,
   "message": "Registration Complete.",
   "dataType" : "user",
   "data": {                
            "username": "user88",
            "email": "[email protected]",
            "created_on": "1426171225",
            "last_login": null,
            "active": "1",
            "first_name": "User",
            "last_name": "",
            "company": null,
            "phone": null,
            "sign_up_mode": "GOOGLE_PLUS"
    }
}

MyResponseクラスには新しいファイルDataTypeが必要なので、次のようになります。

public class MyResponse<T> implements Serializable{
    private boolean status ;
    private String message ;
    private DataType dataType ;
    private T data;


    public DataType getDataType() {
        return dataType;
    }

    //... other getters and setters
}

DataTypeは、データのタイプを定義する列挙型です。 Data.classをコンストラクターのparamとして渡す必要があります。すべてのデータ型について、新しいクラスを作成する必要があります。 DataType列挙型は次のようになります。

public enum DataType {

    @SerializedName("user")
    USER(MeUser.class),
    @SerializedName("product")
    Product(Product.class),
    //other types in the same way, the important think is that 
    //the SerializedName value should be the same as dataType value from json 
    ;


    Type type;

    DataType(Type type) {
        this.type = type;
    }

    public Type getType(){
        return type;
    }
}

Jsonのdesarializatorは次のようになります。

public class DeserializerJson implements JsonDeserializer<MyResponse> {

    @Override
    public MyResponse deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
            throws JsonParseException {
        JsonObject content = je.getAsJsonObject();
        MyResponse message = new Gson().fromJson(je, type);
        JsonElement data = content.get("data");
        message.setData(new Gson().fromJson(data, message.getDataType().getType()));
        return message;

    }
}

そして、RestAdapterを作成するとき、Deserializatorを登録する行で、これを使用する必要があります:

 .registerTypeAdapter(MyResponse.class, new DeserializerJson())

分離されたクラスのGsonの標準POJOのように定義する他のクラス(データのタイプ)。

24

問題は、data属性がTとして定義されているためです。これは、タイプMeUserProductなどであると予想されますが、実際にはオブジェクトです。これはuserのような内部属性を持っています。これを解決するには、必要な属性userproductinvoiceなどを持つ別のレベルのクラスを導入する必要があります。これは、静的内部クラスを使用して簡単に実現できます。

public class MeUser{
  private User user;
  public static class User{
    private String username;
    //add other attributes of the User class
  }
}
1
Rajesh