Instantly share code, notes, and snippets.
CreatedMay 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
| publicstaticclassAndroidFieldNamingPolicyimplementsFieldNamingStrategy { | |
| privatestaticfinalStringJSON_WORD_DELIMITER ="_"; | |
| @Override | |
| publicStringtranslateName(finalFieldf) { | |
| if (f.getName().startsWith("m")) { | |
| returnhandleWords(f.getName().substring(1)); | |
| } | |
| else { | |
| thrownewIllegalArgumentException("Don't know how to handle field not starting with m prefix: " +f.getName()); | |
| } | |
| } | |
| privatefinalstaticPatternUPPERCASE_PATTERN =Pattern.compile("(?=\\p{Lu})"); | |
| privateStringhandleWords(finalStringfieldName) { | |
| String[]words =UPPERCASE_PATTERN.split(fieldName); | |
| finalStringBuffersb =newStringBuffer(); | |
| for (Stringword :words) { | |
| if (sb.length() >0) { | |
| sb.append(JSON_WORD_DELIMITER); | |
| } | |
| sb.append(word.toLowerCase()); | |
| } | |
| returnsb.toString(); | |
| } | |
| } |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment