Skip to content

Commit

Permalink
Merge pull request google#652 from schlan/fix_turkish_locale_issues
Browse files Browse the repository at this point in the history
Fix issues if runing in an environment with a Turkish locale
  • Loading branch information
inder123 committed Aug 8, 2015
2 parents f73f8e0 + 6e57df7 commit 0a93efa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
5 changes: 3 additions & 2 deletions gson/src/main/java/com/google/gson/FieldNamingPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.gson;

import java.lang.reflect.Field;
import java.util.Locale;

/**
* An enumeration that defines a few standard naming conventions for JSON field names.
Expand Down Expand Up @@ -88,7 +89,7 @@ public String translateName(Field f) {
*/
LOWER_CASE_WITH_UNDERSCORES() {
public String translateName(Field f) {
return separateCamelCase(f.getName(), "_").toLowerCase();
return separateCamelCase(f.getName(), "_").toLowerCase(Locale.ENGLISH);
}
},

Expand All @@ -111,7 +112,7 @@ public String translateName(Field f) {
*/
LOWER_CASE_WITH_DASHES() {
public String translateName(Field f) {
return separateCamelCase(f.getName(), "-").toLowerCase();
return separateCamelCase(f.getName(), "-").toLowerCase(Locale.ENGLISH);
}
};

Expand Down
29 changes: 19 additions & 10 deletions gson/src/test/java/com/google/gson/functional/FieldNamingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,60 @@
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_WITH_SPACES;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import junit.framework.TestCase;

public final class FieldNamingTest extends TestCase {
public void testIdentity() {
Gson gson = new GsonBuilder().setFieldNamingPolicy(IDENTITY).create();
Gson gson = getGsonWithNamingPolicy(IDENTITY);
assertEquals("{'lowerCamel':1,'UpperCamel':2,'_lowerCamelLeadingUnderscore':3," +
"'_UpperCamelLeadingUnderscore':4,'lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7}",
"'annotatedName':7,'lowerId':8}",
gson.toJson(new TestNames()).replace('\"', '\''));
}

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," +
"'_UpperCamelLeadingUnderscore':4,'Lower_words':5,'UPPER_WORDS':6," +
"'annotatedName':7}",
"'annotatedName':7,'LowerId':8}",
gson.toJson(new TestNames()).replace('\"', '\''));
}

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," +
"'_ 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('\"', '\''));
}

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," +
"'__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('\"', '\''));
}

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," +
"'_-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('\"', '\''));
}

private Gson getGsonWithNamingPolicy(FieldNamingPolicy fieldNamingPolicy){
return new GsonBuilder()
.setFieldNamingPolicy(fieldNamingPolicy)
.create();
}

@SuppressWarnings("unused") // fields are used reflectively
private static class TestNames {
int lowerCamel = 1;
Expand All @@ -76,5 +84,6 @@ private static class TestNames {
int lower_words = 5;
int UPPER_WORDS = 6;
@SerializedName("annotatedName") int annotated = 7;
int lowerId = 8;
}
}

0 comments on commit 0a93efa

Please sign in to comment.