Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added UPPER_CASE_WITH_UNDERSCORES in FieldNamingPolicy #2024

Merged
merged 7 commits into from Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions gson/src/main/java/com/google/gson/FieldNamingPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
}
},

/**
This conversation was marked as resolved.
Show resolved Hide resolved
* Using this naming policy with Gson will modify the Java Field name from its camel cased
* form to a upper case field name where each word is separated by an underscore (_).
This conversation was marked as resolved.
Show resolved Hide resolved
*
* <p>Here's a few examples of the form "Java Field Name" ---&gt; "JSON Field Name":</p>
This conversation was marked as resolved.
Show resolved Hide resolved
* <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 (_).
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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