Skip to content

Instantly share code, notes, and snippets.

@RareScrap
Created May 16, 2020 07:20
Show Gist options
  • Save RareScrap/98571ddb4ab761c0682ccfd6445ce759 to your computer and use it in GitHub Desktop.
Save RareScrap/98571ddb4ab761c0682ccfd6445ce759 to your computer and use it in GitHub Desktop.
Позволяет GSON'у толкать поля с переменные с префикса "m"
public static class AndroidFieldNamingPolicy implements FieldNamingStrategy {
private static final String JSON_WORD_DELIMITER = "_";
@Override
public String translateName(final Field f) {
if (f.getName().startsWith("m")) {
return handleWords(f.getName().substring(1));
}
else {
throw new IllegalArgumentException("Don't know how to handle field not starting with m prefix: " + f.getName());
}
}
private final static Pattern UPPERCASE_PATTERN = Pattern.compile("(?=\\p{Lu})");
private String handleWords(final String fieldName) {
String[] words = UPPERCASE_PATTERN.split(fieldName);
final StringBuffer sb = new StringBuffer();
for (String word : words) {
if (sb.length() > 0) {
sb.append(JSON_WORD_DELIMITER);
}
sb.append(word.toLowerCase());
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment