deserialization of a JSON API response with moshi

2019-06-03 08:52发布

问题:

I got a null object attributes after deserialization of a json response. Developing under android, I'm using retrofit2 , moshi as converter (https://github.com/kamikat/moshi-jsonapi ) . When debugging ,I saw a json response fully retrieved (not null attributes),but deserialization fails. Should I use GSON instead?

Here's my retrofit builder I use to make my json call: (no issue)

public static JsonServerInterface getSimpleClient(){

     Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_AUTH_URL)a
            .addConverterFactory(MoshiConverterFactory.create())
            .build();
   JsonServerInterface webServer=retrofit.create(JsonServerInterface.class);
   return webServer;     
}

My api json call,response contain UserModel with null attributes(deserialization fails without any error)

signInCall.enqueue(new Callback<UserModel>(){
  @Override
  public void onResponse
  (Call<UserModel> call, Response<UserModel> response)
  {
    response.message();
  }
}

My UserModel (as required by moshi ,but I think it lacks something):

@JsonApi(type = "users")
public class UserModel extends Resource {

@Json(name = "auth-token")
private String authToken;
@Json(name = "firstname")
private String firstname;
@Json(name = "lastname")
private String lastname;
@Json(name = "email")
private String email;
@Json(name = "created-at")
private String createdAt;
@Json(name = "updated-at")
private String updatedAt;

private HasMany<ActivityModel> activities;

My json response I saw when debugging http response, I retrieve without any trouve,but moshi sucks to deserialize it,and no errors are raised:

{
    "data": {
        "id": "21",
        "type": "users",
        "attributes": {
            "auth-token": "t8S3BTqyPwN3T4QDMY1FwEMF",
            "firstname": "aymen",
            "lastname": "myself",
            "email": "aymen.myself@gmail.com",
            "created-at": "2017-11-13T22:52:39.477Z",
            "updated-at": "2017-11-13T23:21:09.706Z"
        },
        "relationships": {
            "activities": {
                "data": [
                    {
                        "id": "81",
                        "type": "activities"
                    }
                ]
            }
        }
    },
    "included": [
        {
            "id": "81",
            "type": "activities",
            "attributes": {
                "title": "activity 10",
                "description": "how to draw a circle",
                "start-at": "2017-11-13T23:06:13.474Z",
                "duration": 10,
                "created-at": "2017-11-13T23:06:32.630Z",
                "updated-at": "2017-11-13T23:06:32.630Z"
            },
            "relationships": {
                "user": {
                    "data": {
                        "id": "21",
                        "type": "users"
                    }
                }
            }
        }
    ]
}

回答1:

I find the solution after lot of hours: I should use "Document" instead of UserModel

interface:

 @POST("sign-in.json")
    Call<Document> signIn(@Body Credentials credentials);

when calling:

   signInCall.enqueue(new Callback<Document>(){
            @Override
            public void onResponse(Call<Document> call, Response<Document> response) {

hope it helps