From f43d910736a96c292b284a8b311e8c236721a57f Mon Sep 17 00:00:00 2001 From: Yixing Zheng Date: Sun, 21 Nov 2021 21:43:27 -0500 Subject: [PATCH 1/6] feat: added UPPER_CASE_WITH_UNDERSCORES for FieldNamingPolicy --- .../com/google/gson/FieldNamingPolicy.java | 18 ++++++++++++++++++ .../com/google/gson/FieldNamingPolicyTest.java | 3 ++- .../gson/functional/FieldNamingTest.java | 9 +++++++++ .../gson/functional/NamingPolicyTest.java | 16 ++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index 16e7124f45..a8a35ede89 100644 --- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java @@ -75,6 +75,24 @@ public enum FieldNamingPolicy implements FieldNamingStrategy { } }, + /** + * 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 (_). + * + *

Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":

+ * + */ + 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 (_). diff --git a/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java b/gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java index a62bae3aad..4d4c716b1e 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 @@ 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"); 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 4e383ec83a..04ba7b7cbe 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_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 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," + 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 5b1bba5beb..ab76e64918 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 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); From 837c4263b1a7553d2f5e3c46e3c22ab5f61262a8 Mon Sep 17 00:00:00 2001 From: Yixing Zheng Date: Mon, 22 Nov 2021 18:03:29 -0500 Subject: [PATCH 2/6] fix: fixed indentation --- gson/src/main/java/com/google/gson/FieldNamingPolicy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index a8a35ede89..b0d9174157 100644 --- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java @@ -75,7 +75,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 upper case field name where each word is separated by an underscore (_). * From 05d2f782b5bd49ddc26a6dc790beaf2a22067793 Mon Sep 17 00:00:00 2001 From: Yixing Zheng Date: Mon, 22 Nov 2021 21:47:27 -0500 Subject: [PATCH 3/6] fix: fixed comment grammar --- .../main/java/com/google/gson/FieldNamingPolicy.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index b0d9174157..d8ee7983a9 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 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 few examples of the form "Java Field Name" ---> "JSON Field Name":

    *
      *
    • someFieldName ---> Some Field Name
    • *
    • _someFieldName ---> _Some Field Name
    • @@ -97,7 +97,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 an underscore (_). * - *

      Here's a few examples of the form "Java Field Name" ---> "JSON Field Name":

      + *

      Here are few examples of the form "Java Field Name" ---> "JSON Field Name":

      *
        *
      • someFieldName ---> some_field_name
      • *
      • _someFieldName ---> _some_field_name
      • @@ -115,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 few examples of the form "Java Field Name" ---> "JSON Field Name":

        *
          *
        • someFieldName ---> some-field-name
        • *
        • _someFieldName ---> _some-field-name
        • @@ -138,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 few examples of the form "Java Field Name" ---> "JSON Field Name":

          *
            *
          • someFieldName ---> some.field.name
          • *
          • _someFieldName ---> _some.field.name
          • From 15efddf5139ebb394396a8e10990ade24274ac55 Mon Sep 17 00:00:00 2001 From: Yixing Zheng Date: Mon, 22 Nov 2021 21:49:30 -0500 Subject: [PATCH 4/6] fix: fixed doc --- gson/src/main/java/com/google/gson/FieldNamingPolicy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index d8ee7983a9..552b1eebaf 100644 --- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java @@ -77,7 +77,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 upper case field name where each word is separated by an underscore (_). + * form to an upper 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":

            *
              From 0622367f51fbd5e9ff748562df103abd595cccaf Mon Sep 17 00:00:00 2001 From: Yixing Zheng Date: Mon, 22 Nov 2021 21:50:50 -0500 Subject: [PATCH 5/6] fix: fixed comment --- gson/src/main/java/com/google/gson/FieldNamingPolicy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index 552b1eebaf..ae707e651e 100644 --- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java @@ -79,7 +79,7 @@ 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's a few examples of the form "Java Field Name" ---> "JSON Field Name":

              + *

              Here are few examples of the form "Java Field Name" ---> "JSON Field Name":

              *
                *
              • someFieldName ---> SOME_FIELD_NAME
              • *
              • _someFieldName ---> _SOME_FIELD_NAME
              • From b63e52c32888a9a7a4aaa2a2085efb82fa9545c8 Mon Sep 17 00:00:00 2001 From: Yixing Zheng Date: Thu, 25 Nov 2021 19:56:56 -0500 Subject: [PATCH 6/6] fix: update it to "here are a few examples" --- .../main/java/com/google/gson/FieldNamingPolicy.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index ae707e651e..a4fa7c2715 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 are 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 are 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
                  • @@ -79,7 +79,7 @@ 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 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 +97,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 an underscore (_). * - *

                      Here are 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
                      • @@ -115,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 are 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
                        • @@ -138,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 are 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