rharter / auto-value-gson

AutoValue Extension to add Gson De/Serializer support
Apache License 2.0
603 stars 103 forks source link

@GsonTypeAdapter is ignored since 0.7.0 #174

Closed MRezaNasirloo closed 5 years ago

MRezaNasirloo commented 6 years ago

I have a custom TypeAdapter<String> which is responsible for parsing an object and return a string value, looks like the processor uses the default String type adapter for this field.

Model class:

@AutoValue
public abstract class UserTrakt {
    @SerializedName("username")
    public abstract String username();

    @Nullable
    @SerializedName("name")
    public abstract String name();

    @SerializedName("ids")
    @GsonTypeAdapter(TypeAdapterIdsToString.class)
    public abstract String id();

    public static TypeAdapter<UserTrakt> typeAdapter(Gson gson) {
        return new AutoValue_UserTrakt.GsonTypeAdapter(gson);
    }
}

My custom TypeAdapter:

class TypeAdapterIdsToString extends TypeAdapter<String> {

    @Override
    public void write(JsonWriter out, String value) throws IOException {
        /* ... */
    }

    @Override
    public String read(JsonReader in) throws IOException {
        /* ... */
        return "parsed id from a large json block";
    }
}

Generated class with 0.7.0

final class AutoValue_UserTrakt extends $AutoValue_UserTrakt {
  AutoValue_UserTrakt(String username, @Nullable String name, String id) {
    super(username, name, id);
  }

  public static final class GsonTypeAdapter extends TypeAdapter<UserTrakt> {
    private final TypeAdapter<String> string_adapter;
    public GsonTypeAdapter(Gson gson) {
      this.string_adapter = gson.getAdapter(String.class);// <== Uses the default implemention for all fields
    }

   /* ... */

}

Generated with 0.6.0

final class AutoValue_UserTrakt extends $AutoValue_UserTrakt {
  AutoValue_UserTrakt(String username, @Nullable String name, String id) {
    super(username, name, id);
  }

  public static final class GsonTypeAdapter extends TypeAdapter<UserTrakt> {
    private final TypeAdapter<String> usernameAdapter;
    private final TypeAdapter<String> nameAdapter;
    private final TypeAdapter<String> idAdapter;
    public GsonTypeAdapter(Gson gson) {
      this.usernameAdapter = gson.getAdapter(String.class);
      this.nameAdapter = gson.getAdapter(String.class);
      this.idAdapter = new TypeAdapterIdsToString();// <== Uses the right adapter here.
    }

  /* ... */

}
ZacSweers commented 5 years ago

@illarionov it looks like you had a fix for this in 9740916. Would you be interested in PR-ing it, or if I crafted a PR out of it?

illarionov commented 5 years ago

@ZacSweers I'd rather you do it since you have more experience with this project's code

ZacSweers commented 5 years ago

Will do!

ZacSweers commented 5 years ago

After talking with @rharter, we're just going to remove this API instead. See #199