Skip to content

Instantly share code, notes, and snippets.

@writtmeyer
Last active February 16, 2019 22:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save writtmeyer/b9b643f1b479f678c220 to your computer and use it in GitHub Desktop.
Save writtmeyer/b9b643f1b479f678c220 to your computer and use it in GitHub Desktop.
If you want to keep your domain objects free from GSON annotations, this is a possible way to do this.
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new DateTypeAdapter())
.setFieldNamingStrategy(new FieldNamingStrategy() {
@Override
public String translateName(Field f) {
if (f.getName().equals("stars")) {
return "stargazers_count";
} else if (f.getName().equals("repositories")) {
return "items";
} else {
return FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES.translateName(f);
}
}
})
.create();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment