Created
May 16, 2020 07:20
-
-
Save RareScrap/98571ddb4ab761c0682ccfd6445ce759 to your computer and use it in GitHub Desktop.
Позволяет GSON'у толкать поля с переменные с префикса "m"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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