diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java
index 16e7124f..a4fa7c27 100644
--- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java
+++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java
@@ -44,7 +44,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* Using this naming policy with Gson will ensure that the first "letter" of the Java
* field name is capitalized when serialized to its JSON form.
*
- *
Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":
+ * Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":
*
* - someFieldName ---> SomeFieldName
* - _someFieldName ---> _SomeFieldName
@@ -61,7 +61,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* field name is capitalized when serialized to its JSON form and the words will be
* separated by a space.
*
- * Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":
+ * Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":
*
* - someFieldName ---> Some Field Name
* - _someFieldName ---> _Some Field Name
@@ -75,11 +75,29 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
}
},
+ /**
+ * Using this naming policy with Gson will modify the Java Field name from its camel cased
+ * form to an upper case field name where each word is separated by an underscore (_).
+ *
+ * Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":
+ *
+ * - someFieldName ---> SOME_FIELD_NAME
+ * - _someFieldName ---> _SOME_FIELD_NAME
+ * - aStringField ---> A_STRING_FIELD
+ * - aURL ---> A_U_R_L
+ *
+ */
+ UPPER_CASE_WITH_UNDERSCORES() {
+ @Override public String translateName(Field f) {
+ return separateCamelCase(f.getName(), '_').toUpperCase(Locale.ENGLISH);
+ }
+ },
+
/**
* Using this naming policy with Gson will modify the Java Field name from its camel cased
* form to a lower case field name where each word is separated by an underscore (_).
*
- * Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":
+ * Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":
*
* - someFieldName ---> some_field_name
* - _someFieldName ---> _some_field_name
@@ -97,7 +115,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* Using this naming policy with Gson will modify the Java Field name from its camel cased
* form to a lower case field name where each word is separated by a dash (-).
*
- * Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":
+ * Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":
*
* - someFieldName ---> some-field-name
* - _someFieldName ---> _some-field-name
@@ -120,7 +138,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* Using this naming policy with Gson will modify the Java Field name from its camel cased
* form to a lower case field name where each word is separated by a dot (.).
*
- * Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":
+ * Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":
*
* - someFieldName ---> some.field.name
* - _someFieldName ---> _some.field.name
diff --git a/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java b/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
index a62bae3a..4d4c716b 100644
--- a/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
+++ b/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
@@ -67,7 +67,8 @@ public class FieldNamingPolicyTest {
FieldNamingPolicy[] policies = {
FieldNamingPolicy.UPPER_CAMEL_CASE,
- FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES
+ FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES,
+ FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES,
};
Field field = Dummy.class.getDeclaredField("i");
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 4e383ec8..04ba7b7c 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,7 @@ 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 static com.google.gson.FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
@@ -53,6 +54,14 @@ public final class FieldNamingTest extends TestCase {
gson.toJson(new TestNames()).replace('\"', '\''));
}
+ public void testUpperCaseWithUnderscores() {
+ Gson gson = getGsonWithNamingPolicy(UPPER_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,'_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," +
diff --git a/gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java b/gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java
index 5b1bba5b..ab76e649 100644
--- a/gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java
+++ b/gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java
@@ -141,6 +141,22 @@ public class NamingPolicyTest extends TestCase {
assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
}
+ public void testGsonWithUpperCaseUnderscorePolicySerialization() {
+ Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES)
+ .create();
+ StringWrapper target = new StringWrapper("blah");
+ assertEquals("{\"SOME_CONSTANT_STRING_INSTANCE_FIELD\":\""
+ + target.someConstantStringInstanceField + "\"}", gson.toJson(target));
+ }
+
+ public void testGsonWithUpperCaseUnderscorePolicyDeserialiation() {
+ Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES)
+ .create();
+ String target = "{\"SOME_CONSTANT_STRING_INSTANCE_FIELD\":\"someValue\"}";
+ StringWrapper deserializedObject = gson.fromJson(target, StringWrapper.class);
+ assertEquals("someValue", deserializedObject.someConstantStringInstanceField);
+ }
+
public void testDeprecatedNamingStrategy() throws Exception {
Gson gson = builder.setFieldNamingStrategy(new UpperCaseNamingStrategy()).create();
ClassWithDuplicateFields target = new ClassWithDuplicateFields(10);