Skip to content

Commit

Permalink
feat: added UPPER_CASE_WITH_UNDERSCORES in FieldNamingPolicy (#2024)
Browse files Browse the repository at this point in the history
  • Loading branch information
yixingz3 committed Nov 28, 2021
1 parent 16b42ff commit eaf9a03
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
28 changes: 23 additions & 5 deletions gson/src/main/java/com/google/gson/FieldNamingPolicy.java
Expand Up @@ -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.
*
* <p>Here's a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <p>Here are a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <ul>
* <li>someFieldName ---&gt; SomeFieldName</li>
* <li>_someFieldName ---&gt; _SomeFieldName</li>
Expand All @@ -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.
*
* <p>Here's a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <p>Here are a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <ul>
* <li>someFieldName ---&gt; Some Field Name</li>
* <li>_someFieldName ---&gt; _Some Field Name</li>
Expand All @@ -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 (_).
*
* <p>Here are a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <ul>
* <li>someFieldName ---&gt; SOME_FIELD_NAME</li>
* <li>_someFieldName ---&gt; _SOME_FIELD_NAME</li>
* <li>aStringField ---&gt; A_STRING_FIELD</li>
* <li>aURL ---&gt; A_U_R_L</li>
* </ul>
*/
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 (_).
*
* <p>Here's a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <p>Here are a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <ul>
* <li>someFieldName ---&gt; some_field_name</li>
* <li>_someFieldName ---&gt; _some_field_name</li>
Expand All @@ -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 (-).
*
* <p>Here's a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <p>Here are a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <ul>
* <li>someFieldName ---&gt; some-field-name</li>
* <li>_someFieldName ---&gt; _some-field-name</li>
Expand All @@ -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 (.).
*
* <p>Here's a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <p>Here are a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
* <ul>
* <li>someFieldName ---&gt; some.field.name</li>
* <li>_someFieldName ---&gt; _some.field.name</li>
Expand Down
Expand Up @@ -67,7 +67,8 @@ class Dummy {

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");
Expand Down
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -53,6 +54,14 @@ public void testUpperCamelCaseWithSpaces() {
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," +
Expand Down
Expand Up @@ -141,6 +141,22 @@ public void testGsonWithUpperCamelCaseSpacesPolicyDeserialiation() {
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);
Expand Down

0 comments on commit eaf9a03

Please sign in to comment.