Comments (20)
We finally got around to releasing Gson 2.7... 🎉
from auto-value-gson.
For now I'm overriding AutoValue's abstract generated methods and using @SerializedName annotation. It's ugly and adds unnecessary code lines, but it works until this issue is fixed
@Override
@SerializedName("full_name")
public abstract String fullName();
from auto-value-gson.
I agree this, in addition to the date format, etc, would be useful to have automatically handled in the generated TypeAdapterFactory.
Unfortunately, the FieldNamingPolicy that's set in the GsonBuilder
is not available, so I think the only real option we have is to add an optional constructor to the generated TypeAdapterFactory that takes these settings, or a config object containing them.
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapterFactory(
Foo.typeAdapterFactory(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES))))
.build();
This, obviously, can get quite unweildy, but could be reduced by the solution in issue #19.
from auto-value-gson.
@rharter have you tried that solution? I can't make it work
This is my TypeAdapterFactory
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<? super T> rawType = type.getRawType();
if (rawType.equals(Foo.class)) {
return (TypeAdapter<T>) Foo.typeAdapter(new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create());
}
return null;
}
I've checked the Gson object the TypeAdapter uses and I do see the FieldNamingPolicy in Gson.factories, but I don't see where it should be invoked, and jsonReader.nextName() still returns the lowercase name instead of camelCase.
Thanks!
from auto-value-gson.
Yeah, the one in Gson.factories is used internally, but isn't available to custom TypeAdapters, IIRC. Even if it was, we'd have to update the generated TypeAdapter to use it.
from auto-value-gson.
Then there's no way to use a custom FieldNamingPolicy? That's a blocking issue for those of us that do need it.
Thanks anyway for your time, I'll keep an eye on this issue hoping that it gets fixed soon.
from auto-value-gson.
@Maragues I just submitted a PR to Gson to support this.
from auto-value-gson.
Wow, that was quick! Let's see how that works out.
from auto-value-gson.
@rharter I assume this is blocked until Gson publishes a new stable release. Will it be fixed easily once they do?
I've been avoiding the issue up until now, but I guess I'll need to fork the repo and force the fieldNamingPolicy
from auto-value-gson.
yeah, should be fairly straightforward once that's up
from auto-value-gson.
No news about a fix for this issue ?
from auto-value-gson.
What will the generated code for this look like? FieldNamingPolicy expects java.lang.reflect.Field
s, so will the generated code have to look up the fields reflectively and keep track of the mapping at runtime?
It'd be nice if there was a way to configure the policy statically, and preserve the current string switch code.
from auto-value-gson.
Just wondering if there has been any progress on this issue?
from auto-value-gson.
Any news? It feels like auto value is not so great after all...
from auto-value-gson.
Maragues' answer is the correct one IMO, sans overriding (not sure what you mean?). Just annotate the property in your AutoValue class. I don't see why auto-value-gson needs to take an opinion on this.
from auto-value-gson.
ICYMI: I opened #124 as a proposal to add this feature, appreciate any feedback.
Biggest limitation (or feature?) is that it doesn't use reflection, so can't handle arbitrary FieldNameStrategy
s, but it does support all the built in FieldNamePolicy
s that Gson provides.
from auto-value-gson.
@rharter seems your PR to google/gson has been merged to 2.8.1. When can the impl for TypedAdapter be made to enable setting field name strategy?
from auto-value-gson.
I'm pretty against av-gson taking an opinion here when you can just annotate parameters. This feature of gson in general was always a bit weird to me since it's pretty best effort
from auto-value-gson.
hzsweers it is a convenient tool for auto mapping the names instead of writing serialized annotations, which makes the class less readable i think.
from auto-value-gson.
why not have your backend send you nicer keys? Also - the only thing you need to read are your methods when using the model code from elsewhere, the annotations are an implementation detail, I don't know what readability has to do with it
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.