0

I am using autovalue gson to serialize and deserialize java objects. I have introduced a response class model which indicates a model corresponding to a response of an API.

However, when I am trying to test the json deserialization I am constantly getting exception: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 15 path $.response

// Response.java class
@GenerateTypeAdapter
@AutoValue
public abstract class Response {

  // Populated in case of error scenarios.
  public abstract Optional<String> response();

  public abstract String apiResponse();

  public abstract Boolean apiSuccess();

  public abstract Optional<MyFirstClass> firstInfo();

  public abstract Optional<String> message();


  public abstract Builder toBuilder();

  public static Builder builder() {
    return new AutoValue_Response.Builder();
  }

  public static TypeAdapter<Response> typeAdapter(Gson gson) {
    return new Response_GsonTypeAdapter(gson);
  }

  /** Builder for {@link Response}. */
  @AutoValue.Builder
  public abstract static class Builder {
   // All the setters and build method definitions.
  }
}
// AutoValueGsonTypeAdapterFactory.java class
@GsonTypeAdapterFactory
public abstract class AutoValueGsonTypeAdapterFactory implements TypeAdapterFactory {
  public static TypeAdapterFactory create() {
    return new AutoValueGson_AutoValueGsonTypeAdapterFactory();
  }
}

I am using the deserialization like this:

// In UT, I am testing like below:
private static final String failureResponseJson =
  "{\"response\": \"Cannot process now\",\"apiResponse\":\"99\",\"apiSuccess\":\"false\",\"message\":\"Cannot process now\"}";
Gson gson = new GsonBuilder()
  .registerTypeAdapterFactory(AutoValueGsonTypeAdapterFactory.create())
  .create();

CreateDisputeJuspayResponse response = gson.fromJson(failureResponseJson, CreateDisputeJuspayResponse.class)

However, I am getting java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 15 path $.response continuously. I also tried with various other json strings, but none seem to be working. Any help would be highly appreciated.

Sunil Kumar
  • 390
  • 1
  • 7
  • 25

0 Answers0