From 299ee89852f8e3ac510a939a2583515a6f1c3e87 Mon Sep 17 00:00:00 2001 From: Sebastian Chlan Date: Tue, 9 Jun 2015 15:12:38 +0100 Subject: [PATCH 1/2] Add tests to demonstrate the issue Run the FieldNamingTest with the JVM options: `-Duser.language=tr -Duser.region=TR` --- .../gson/functional/FieldNamingTest.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/gson/src/test/java/com/google/gson/functional/FieldNamingTest.java b/gson/src/test/java/com/google/gson/functional/FieldNamingTest.java index 54db150c..5d326af8 100644 --- a/gson/src/test/java/com/google/gson/functional/FieldNamingTest.java +++ b/gson/src/test/java/com/google/gson/functional/FieldNamingTest.java @@ -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.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; @@ -28,45 +30,51 @@ 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; @@ -76,5 +84,6 @@ public final class FieldNamingTest extends TestCase { int lower_words = 5; int UPPER_WORDS = 6; @SerializedName("annotatedName") int annotated = 7; + int lowerId = 8; } } From 6e57df7e9648f4a48b500ac9a181a058829bcee1 Mon Sep 17 00:00:00 2001 From: Sebastian Chlan Date: Tue, 9 Jun 2015 15:17:08 +0100 Subject: [PATCH 2/2] FieldNamingPolicy: Use Locale.ENGLISH to be locale insensitive --- gson/src/main/java/com/google/gson/FieldNamingPolicy.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index 21863f9c..6b4c72ca 100644 --- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java @@ -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. @@ -88,7 +89,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy { */ LOWER_CASE_WITH_UNDERSCORES() { 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() { public String translateName(Field f) { - return separateCamelCase(f.getName(), "-").toLowerCase(); + return separateCamelCase(f.getName(), "-").toLowerCase(Locale.ENGLISH); } };