Giter Club home page Giter Club logo

Comments (8)

jachenry avatar jachenry commented on July 2, 2024 2

Below is a quick solution for anyone else that hits this before #23 is completed.

@GsonTypeAdapterFactory
public abstract class AutoValueAdapterFactory implements TypeAdapterFactory {

  public static TypeAdapterFactory create() {
    final TypeAdapterFactory factory = new AutoValueGson_AutoValueAdapterFactory();

    return new TypeAdapterFactory() {
      @Override
      public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        TypeAdapter<T> typeAdapter = factory.create(gson, type);
        return typeAdapter != null ? typeAdapter.nullSafe() : null;
      }
    };
  }
}

from auto-value-gson.

rharter avatar rharter commented on July 2, 2024

You could submit a PR if you've got the code. Otherwise I'll have to add that.

from auto-value-gson.

muddin1 avatar muddin1 commented on July 2, 2024

Are you saying that we should attach nullSafe() to every AutoValue typeAdapter mentioned in the generated Factory i.e. in the annotation processor?

Also, there's this PR #23 that presumably eliminates the need to use nullSafe() completely.

from auto-value-gson.

muddin1 avatar muddin1 commented on July 2, 2024

@jachenry Thanks, I've been doing the same.

from auto-value-gson.

jjimenez0611 avatar jjimenez0611 commented on July 2, 2024

Hi @jachenry

Please can you explain to me with more details..

I have my factory like this:

public class AutoValueGsonTyeAdapterFactory implements TypeAdapterFactory {

public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<? super T> rawType = type.getRawType();

    if (rawType.equals(MGuest.class)) {
        return (TypeAdapter<T>) MGuest.typeAdapter(gson);
    }

    if(rawType.equals(MTicket.class)){
        return (TypeAdapter<T>) MTicket.typeAdapter(gson);
    }

    return null;
}

}

Then I have your abstract class that contains my class

@GsonTypeAdapterFactory
public abstract class AutoValueAdapterFactory implements TypeAdapterFactory {

public static TypeAdapterFactory create() {
    final TypeAdapterFactory factory = new **AutoValueGsonTyeAdapterFactory();**

    return new TypeAdapterFactory() {
        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            TypeAdapter<T> typeAdapter = factory.create(gson, type);
            return typeAdapter != null ? typeAdapter.nullSafe() : null;
        }
    };
}

}

And finally I need to call this classes in my GsonConverterFactory, but in this point I really don't now how to call the abstract class...

public static GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(
new GsonBuilder()
.registerTypeAdapterFactory(MyTypeAdapterFactory)
.create());

Can you help me in this last step?

from auto-value-gson.

jachenry avatar jachenry commented on July 2, 2024

@jjimenez0611 You'll want to call the static create() method on AutoValueAdapterFactory.

@GsonTypeAdapterFactory
public abstract class AutoValueAdapterFactory implements TypeAdapterFactory {

public static TypeAdapterFactory create() {
    final TypeAdapterFactory factory = new AutoValueGson_AutoValueAdapterFactory();

    return new TypeAdapterFactory() {
        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            TypeAdapter<T> typeAdapter = factory.create(gson, type);
            return typeAdapter != null ? typeAdapter.nullSafe() : null;
        }
    };
}
new GsonBuilder()
    .registerTypeAdapterFactory(AutoValueAdapterFactory.create())
    .create()

from auto-value-gson.

jjimenez0611 avatar jjimenez0611 commented on July 2, 2024

@jachenry, thanks I really appreciate your help, now I understand.

from auto-value-gson.

ZacSweers avatar ZacSweers commented on July 2, 2024

Closed by #78

from auto-value-gson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.