-
Notifications
You must be signed in to change notification settings - Fork 82
-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use with Gson #6
Comments
|
Outgoing serialization will work. Incoming serialization needs pointed at the generated class rather than the model class. private class FooDeserializer implements JsonDeserializer<Foo> {
public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return context.deserialize(json, AutoParcel_Foo.class);
}
} |
I had something similar but is there a way to do it for all model classes rather than registering a JsonDeserializer for each model class? |
What I'm currently doing is look up the class name and prefix |
No. You could write another annotation processor for |
A code generator for a code generator? Nice! |
This is such a common use case that I guess the library should provide an |
For Gson, this looks the preferred way to create an instance: http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/InstanceCreator.html |
Looks like this would be useful here |
@JakeWharton, your solution works for most situations, but what if I want to use Gson annotations like SerializedName? |
That won't work out of the box, you would have to modify AutoParcel to recognize |
@f2prateek yes, I am working on this approach now. |
That's not a very scalable or generalized solution.
|
@JakeWharton what would you recommend instead? |
I don't know. But you can't special Gson because, well, that's just not a reasonable solution. You can't blindly copy annotations from the methods to fields because the Unless this library becomes AutoGsonParcel I don't think this should be done at all. The fact that AutoParcel generates code to fulfill the immutability and |
Personally quite against annotating your model object with anything that has to do with serialisation. The reason being that you complect data and representation of data (Gson, Jackson, Xml, Cursor). I'd like to freely swap between serialisers without re-annotating my model objects. But the truth is those annotations are pretty convenient, so I'd like to find a lean way to provide the same semantic but with a different strategy. Jake, let's see how this scores as a fairly convoluted solution: @GsonKeyMapper(GsonFooMapper.class)
@JacksonKeyMapper(JacksonFooMapper.class)
@ContentValueKeyMapper(ContentValueFooMapper.class)
public class Foo {...} A specific annotation processor could generate the required public abstract class GsonFooMapper extends Foo {
@SerialisedName("x")
public abstract String y();
} That way the serialisation mechanisms are composable and easy to refactor. I wish best of luck to anybody who wants to go down that path, tho. |
Gson also need an empty constructor, right? Those are abstract classes :) |
Not since version 2, no. And we are talking about deserialization using the
|
Gson would not directly construct a |
It's even better if the model object is not annotated at all. @GsonSerializationSpec(Foo.class)
public abstract class FooGsonSpec extends Foo {
@SerialisedName("x")
public abstract String y();
} That way adding a new serialisation option / changing the serialisation logic for your models will leave your model classes untouched. |
@frankiesardo what about this issue, can we expect in the near future the implementation based on |
I think this can be closed since the direction of AutoValue extensions will allow this to be accomplished with composition of extensions. |
Agree. This is now out of scope. |
Could you annotate your model classes with
AutoValue
when using with Gson? Since Gson uses fields directly, I wasn't sure how it would work.The text was updated successfully, but these errors were encountered: