Skip to content

Commit

Permalink
Refactor uppercaseFirstLetter, add additional field to test (#1515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Degubi authored and inder123 committed Apr 26, 2019
1 parent c5a3f21 commit 63ee47c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
31 changes: 10 additions & 21 deletions gson/src/main/java/com/google/gson/FieldNamingPolicy.java
Expand Up @@ -159,31 +159,20 @@ static String separateCamelCase(String name, String separator) {
* Ensures the JSON field names begins with an upper case letter.
*/
static String upperCaseFirstLetter(String name) {
StringBuilder fieldNameBuilder = new StringBuilder();
int index = 0;
char firstCharacter = name.charAt(index);
int length = name.length();
int firstLetterIndex = 0;
int limit = name.length() - 1;
for(; !Character.isLetter(name.charAt(firstLetterIndex)) && firstLetterIndex < limit; ++firstLetterIndex);

while (index < length - 1) {
if (Character.isLetter(firstCharacter)) {
break;
}

fieldNameBuilder.append(firstCharacter);
firstCharacter = name.charAt(++index);
char firstLetter = name.charAt(firstLetterIndex);
if(Character.isUpperCase(firstLetter)) { //The letter is already uppercased, return the original
return name;
}

if (!Character.isUpperCase(firstCharacter)) {
String modifiedTarget = modifyString(Character.toUpperCase(firstCharacter), name, ++index);
return fieldNameBuilder.append(modifiedTarget).toString();
} else {
return name;
char uppercased = Character.toUpperCase(firstLetter);
if(firstLetterIndex == 0) { //First character in the string is the first letter, saves 1 substring
return uppercased + name.substring(1);
}
}

private static String modifyString(char firstCharacter, String srcString, int indexOfSubstring) {
return (indexOfSubstring < srcString.length())
? firstCharacter + srcString.substring(indexOfSubstring)
: String.valueOf(firstCharacter);
return name.substring(0, firstLetterIndex) + uppercased + name.substring(firstLetterIndex + 1);
}
}
Expand Up @@ -33,39 +33,39 @@ public void testIdentity() {
Gson gson = getGsonWithNamingPolicy(IDENTITY);
assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7,'lowerId':8}",
"'annotatedName':7,'lowerId':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\''));
}

public void testUpperCamelCase() {
Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE);
assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7,'LowerId':8}",
"'annotatedName':7,'LowerId':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\''));
}

public void testUpperCamelCaseWithSpaces() {
Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES);
assertEquals("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," +
"'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," +
"'annotatedName':7,'Lower Id':8}",
"'annotatedName':7,'Lower Id':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\''));
}

public void testLowerCaseWithUnderscores() {
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
assertEquals("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," +
"'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," +
"'annotatedName':7,'lower_id':8}",
"'annotatedName':7,'lower_id':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\''));
}

public void testLowerCaseWithDashes() {
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES);
assertEquals("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," +
"'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," +
"'annotatedName':7,'lower-id':8}",
"'annotatedName':7,'lower-id':8,'_9':9}",
gson.toJson(new TestNames()).replace('\"', '\''));
}

Expand All @@ -85,5 +85,6 @@ private static class TestNames {
int UPPER_WORDS = 6;
@SerializedName("annotatedName") int annotated = 7;
int lowerId = 8;
int _9 = 9;
}
}

0 comments on commit 63ee47c

Please sign in to comment.