From ad4ff6996b09e0e1aeda18c3d9edef61c7ec4829 Mon Sep 17 00:00:00 2001 From: Joel Leitch Date: Tue, 29 Sep 2009 18:44:56 +0000 Subject: [PATCH] Adding new default field naming policy --- .../java/com/google/gson/FieldNamingPolicy.java | 16 +++++++++++++++- .../LowerCamelCaseSeparatorNamingPolicyTest.java | 6 ++++++ 2 files changed, 21 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 9c1abc5d..f0a07730 100644 --- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java @@ -51,7 +51,21 @@ public enum FieldNamingPolicy { *
  • aURL ---> a_u_r_l
  • * */ - LOWER_CASE_WITH_UNDERSCORES(new LowerCamelCaseSeparatorNamingPolicy("_")); + LOWER_CASE_WITH_UNDERSCORES(new LowerCamelCaseSeparatorNamingPolicy("_")), + + /** + * 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":

    + * + */ + LOWER_CASE_WITH_DASHES(new LowerCamelCaseSeparatorNamingPolicy("-")); private final FieldNamingStrategy namingPolicy; diff --git a/gson/src/test/java/com/google/gson/LowerCamelCaseSeparatorNamingPolicyTest.java b/gson/src/test/java/com/google/gson/LowerCamelCaseSeparatorNamingPolicyTest.java index d29864ca..7401cf44 100644 --- a/gson/src/test/java/com/google/gson/LowerCamelCaseSeparatorNamingPolicyTest.java +++ b/gson/src/test/java/com/google/gson/LowerCamelCaseSeparatorNamingPolicyTest.java @@ -50,4 +50,10 @@ public class LowerCamelCaseSeparatorNamingPolicyTest extends TestCase { String translatedName = namingPolicy.translateName("aURL", CLASS, null); assertEquals("a_u_r_l", translatedName); } + + public void testUsingDashesInstead() throws Exception { + namingPolicy = new LowerCamelCaseSeparatorNamingPolicy("-"); + String translatedName = namingPolicy.translateName("testUsingDashesInstead", CLASS, null); + assertEquals("test-using-dashes-instead", translatedName); + } }