feat: added UPPER_CASE_WITH_UNDERSCORES in FieldNamingPolicy (#2024)
This commit is contained in:
parent
16b42ff580
commit
eaf9a0342d
@ -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" ---> "JSON Field Name":</p>
|
* <p>Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":</p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>someFieldName ---> SomeFieldName</li>
|
* <li>someFieldName ---> SomeFieldName</li>
|
||||||
* <li>_someFieldName ---> _SomeFieldName</li>
|
* <li>_someFieldName ---> _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" ---> "JSON Field Name":</p>
|
* <p>Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":</p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>someFieldName ---> Some Field Name</li>
|
* <li>someFieldName ---> Some Field Name</li>
|
||||||
* <li>_someFieldName ---> _Some Field Name</li>
|
* <li>_someFieldName ---> _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" ---> "JSON Field Name":</p>
|
||||||
|
* <ul>
|
||||||
|
* <li>someFieldName ---> SOME_FIELD_NAME</li>
|
||||||
|
* <li>_someFieldName ---> _SOME_FIELD_NAME</li>
|
||||||
|
* <li>aStringField ---> A_STRING_FIELD</li>
|
||||||
|
* <li>aURL ---> 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" ---> "JSON Field Name":</p>
|
* <p>Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":</p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>someFieldName ---> some_field_name</li>
|
* <li>someFieldName ---> some_field_name</li>
|
||||||
* <li>_someFieldName ---> _some_field_name</li>
|
* <li>_someFieldName ---> _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" ---> "JSON Field Name":</p>
|
* <p>Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":</p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>someFieldName ---> some-field-name</li>
|
* <li>someFieldName ---> some-field-name</li>
|
||||||
* <li>_someFieldName ---> _some-field-name</li>
|
* <li>_someFieldName ---> _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" ---> "JSON Field Name":</p>
|
* <p>Here are a few examples of the form "Java Field Name" ---> "JSON Field Name":</p>
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>someFieldName ---> some.field.name</li>
|
* <li>someFieldName ---> some.field.name</li>
|
||||||
* <li>_someFieldName ---> _some.field.name</li>
|
* <li>_someFieldName ---> _some.field.name</li>
|
||||||
|
@ -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");
|
||||||
|
@ -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," +
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user