Merge pull request #652 from schlan/fix_turkish_locale_issues

Fix issues if runing in an environment with a Turkish locale
This commit is contained in:
inder123 2015-08-08 09:10:48 -07:00
commit 0a93efada5
2 changed files with 22 additions and 12 deletions

View File

@ -17,6 +17,7 @@
package com.google.gson; package com.google.gson;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Locale;
/** /**
* An enumeration that defines a few standard naming conventions for JSON field names. * An enumeration that defines a few standard naming conventions for JSON field names.
@ -88,7 +89,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
*/ */
LOWER_CASE_WITH_UNDERSCORES() { LOWER_CASE_WITH_UNDERSCORES() {
public String translateName(Field f) { public String translateName(Field f) {
return separateCamelCase(f.getName(), "_").toLowerCase(); return separateCamelCase(f.getName(), "_").toLowerCase(Locale.ENGLISH);
} }
}, },
@ -111,7 +112,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
*/ */
LOWER_CASE_WITH_DASHES() { LOWER_CASE_WITH_DASHES() {
public String translateName(Field f) { public String translateName(Field f) {
return separateCamelCase(f.getName(), "-").toLowerCase(); return separateCamelCase(f.getName(), "-").toLowerCase(Locale.ENGLISH);
} }
}; };

View File

@ -21,6 +21,8 @@ import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_DASHES;
import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES; import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;
import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE; import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE;
import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES; import static com.google.gson.FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
@ -28,45 +30,51 @@ import junit.framework.TestCase;
public final class FieldNamingTest extends TestCase { public final class FieldNamingTest extends TestCase {
public void testIdentity() { public void testIdentity() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(IDENTITY).create(); Gson gson = getGsonWithNamingPolicy(IDENTITY);
assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," + assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," + "'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7}", "'annotatedName':7,'lowerId':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
public void testUpperCamelCase() { public void testUpperCamelCase() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(UPPER_CAMEL_CASE).create(); Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE);
assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," + assertEquals("{'LowerCamel':1,'UpperCamel':2,'_LowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," + "'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7}", "'annotatedName':7,'LowerId':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
public void testUpperCamelCaseWithSpaces() { public void testUpperCamelCaseWithSpaces() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES).create(); Gson gson = getGsonWithNamingPolicy(UPPER_CAMEL_CASE_WITH_SPACES);
assertEquals("{'Lower Camel':1,'Upper Camel':2,'_Lower Camel Leading Underscore':3," + 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," + "'_ Upper Camel Leading Underscore':4,'Lower_words':5,'U P P E R_ W O R D S':6," +
"'annotatedName':7}", "'annotatedName':7,'Lower Id':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
public void testLowerCaseWithUnderscores() { public void testLowerCaseWithUnderscores() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create(); Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
assertEquals("{'lower_camel':1,'upper_camel':2,'_lower_camel_leading_underscore':3," + 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," + "'__upper_camel_leading_underscore':4,'lower_words':5,'u_p_p_e_r__w_o_r_d_s':6," +
"'annotatedName':7}", "'annotatedName':7,'lower_id':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
public void testLowerCaseWithDashes() { public void testLowerCaseWithDashes() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_DASHES).create(); Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_DASHES);
assertEquals("{'lower-camel':1,'upper-camel':2,'_lower-camel-leading-underscore':3," + 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," + "'_-upper-camel-leading-underscore':4,'lower_words':5,'u-p-p-e-r_-w-o-r-d-s':6," +
"'annotatedName':7}", "'annotatedName':7,'lower-id':8}",
gson.toJson(new TestNames()).replace('\"', '\'')); gson.toJson(new TestNames()).replace('\"', '\''));
} }
private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy){
return new GsonBuilder()
.setFieldNamingPolicy(fieldNamingPolicy)
.create();
}
@SuppressWarnings("unused") // fields are used reflectively @SuppressWarnings("unused") // fields are used reflectively
private static class TestNames { private static class TestNames {
int lowerCamel = 1; int lowerCamel = 1;
@ -76,5 +84,6 @@ public final class FieldNamingTest extends TestCase {
int lower_words = 5; int lower_words = 5;
int UPPER_WORDS = 6; int UPPER_WORDS = 6;
@SerializedName("annotatedName") int annotated = 7; @SerializedName("annotatedName") int annotated = 7;
int lowerId = 8;
} }
} }