feat: added UPPER_CASE_WITH_UNDERSCORES in FieldNamingPolicy (#2024)

This commit is contained in:
yixingz3 2021-11-28 12:33:22 -05:00 committed by GitHub
parent 16b42ff580
commit eaf9a0342d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 6 deletions

View File

@ -44,7 +44,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* Using this naming policy with Gson will ensure that the first "letter" of the Java * 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. * 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> * <ul>
* <li>someFieldName ---&gt; SomeFieldName</li> * <li>someFieldName ---&gt; SomeFieldName</li>
* <li>_someFieldName ---&gt; _SomeFieldName</li> * <li>_someFieldName ---&gt; _SomeFieldName</li>
@ -61,7 +61,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
* field name is capitalized when serialized to its JSON form and the words will be * field name is capitalized when serialized to its JSON form and the words will be
* separated by a space. * 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> * <ul>
* <li>someFieldName ---&gt; Some Field Name</li> * <li>someFieldName ---&gt; Some Field Name</li>
* <li>_someFieldName ---&gt; _Some Field Name</li> * <li>_someFieldName ---&gt; _Some Field Name</li>
@ -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 * 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 (_). * 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> * <ul>
* <li>someFieldName ---&gt; some_field_name</li> * <li>someFieldName ---&gt; some_field_name</li>
* <li>_someFieldName ---&gt; _some_field_name</li> * <li>_someFieldName ---&gt; _some_field_name</li>
@ -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 * 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 (-). * 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> * <ul>
* <li>someFieldName ---&gt; some-field-name</li> * <li>someFieldName ---&gt; some-field-name</li>
* <li>_someFieldName ---&gt; _some-field-name</li> * <li>_someFieldName ---&gt; _some-field-name</li>
@ -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 * 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 (.). * 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> * <ul>
* <li>someFieldName ---&gt; some.field.name</li> * <li>someFieldName ---&gt; some.field.name</li>
* <li>_someFieldName ---&gt; _some.field.name</li> * <li>_someFieldName ---&gt; _some.field.name</li>

View File

@ -67,7 +67,8 @@ public class FieldNamingPolicyTest {
FieldNamingPolicy[] policies = { FieldNamingPolicy[] policies = {
FieldNamingPolicy.UPPER_CAMEL_CASE, 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"); Field field = Dummy.class.getDeclaredField("i");

View File

@ -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.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 static com.google.gson.FieldNamingPolicy.UPPER_CASE_WITH_UNDERSCORES;
import com.google.gson.FieldNamingPolicy; import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -53,6 +54,14 @@ public final class FieldNamingTest extends TestCase {
gson.toJson(new TestNames()).replace('\"', '\'')); 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() { public void testLowerCaseWithUnderscores() {
Gson gson = getGsonWithNamingPolicy(LOWER_CASE_WITH_UNDERSCORES); 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," +

View File

@ -141,6 +141,22 @@ public class NamingPolicyTest extends TestCase {
assertEquals("someValue", deserializedObject.someConstantStringInstanceField); 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 { public void testDeprecatedNamingStrategy() throws Exception {
Gson gson = builder.setFieldNamingStrategy(new UpperCaseNamingStrategy()).create(); Gson gson = builder.setFieldNamingStrategy(new UpperCaseNamingStrategy()).create();
ClassWithDuplicateFields target = new ClassWithDuplicateFields(10); ClassWithDuplicateFields target = new ClassWithDuplicateFields(10);