Comments (8)
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.
You could submit a PR if you've got the code. Otherwise I'll have to add that.
from auto-value-gson.
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.
@jachenry Thanks, I've been doing the same.
from auto-value-gson.
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.
@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.
@jachenry, thanks I really appreciate your help, now I understand.
from auto-value-gson.
Closed by #78
from auto-value-gson.
Related Issues (20)
- Parse json object with varying data types
- RV_RETURN_VALUE_IGNORED findbugs error HOT 3
- Build failed in the generated read method for a generic class HOT 1
- Originating element not set HOT 3
- Generate proguard rules HOT 1
- Generic base types cause compile errors HOT 2
- Allow custom opt-in annotation for factory HOT 2
- AutoValueGsonExtension fails on immutable collections. HOT 1
- "Unable to get public no-arg constructor" simply when adding auto-value-gson dependency HOT 3
- auto-value-gson-from-rharter-generated-invalid-code HOT 6
- What dependency AutoValue annotation is in ? HOT 1
- Non-deterministic generation of TypeAdapterFactory HOT 2
- Issue using @GenerateTypeAdapter with maven HOT 2
- `@Nullable` doesn't work correctly with nested types
- NullPointerException processing ImmutableList.Builder Builder method
- Compilation error in Eclipse/maven
- Handling unrecognized json properties HOT 1
- @AutoValue.Builder generates objects with all fields HOT 2
- 也许这个方案更加好 HOT 1
- Could not instantiate annotation processor 'com.ryanharter.auto.value.gson.AutoValueGsonAdapterFactoryProcessor'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from auto-value-gson.