Skip to content

Commit 3bc670e

Browse files
committed
Give FieldNamingStrategy the ability to return multiple String names
1 parent 78caa5e commit 3bc670e

8 files changed

Lines changed: 38 additions & 26 deletions

File tree

gson/src/main/java/com/google/gson/FieldNamingPolicy.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.google.gson;
1818

1919
import java.lang.reflect.Field;
20+
import java.util.Collections;
21+
import java.util.List;
2022
import java.util.Locale;
2123

2224
/**
@@ -33,8 +35,8 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
3335
/** Using this naming policy with Gson will ensure that the field name is unchanged. */
3436
IDENTITY() {
3537
@Override
36-
public String translateName(Field f) {
37-
return f.getName();
38+
public List<String> translateName(Field f) {
39+
return Collections.singletonList(f.getName());
3840
}
3941
},
4042

@@ -51,8 +53,8 @@ public String translateName(Field f) {
5153
*/
5254
UPPER_CAMEL_CASE() {
5355
@Override
54-
public String translateName(Field f) {
55-
return upperCaseFirstLetter(f.getName());
56+
public List<String> translateName(Field f) {
57+
return Collections.singletonList(upperCaseFirstLetter(f.getName()));
5658
}
5759
},
5860

@@ -71,8 +73,8 @@ public String translateName(Field f) {
7173
*/
7274
UPPER_CAMEL_CASE_WITH_SPACES() {
7375
@Override
74-
public String translateName(Field f) {
75-
return upperCaseFirstLetter(separateCamelCase(f.getName(), ' '));
76+
public List<String> translateName(Field f) {
77+
return Collections.singletonList(upperCaseFirstLetter(separateCamelCase(f.getName(), ' ')));
7678
}
7779
},
7880

@@ -93,8 +95,9 @@ public String translateName(Field f) {
9395
*/
9496
UPPER_CASE_WITH_UNDERSCORES() {
9597
@Override
96-
public String translateName(Field f) {
97-
return separateCamelCase(f.getName(), '_').toUpperCase(Locale.ENGLISH);
98+
public List<String> translateName(Field f) {
99+
return Collections.singletonList(
100+
separateCamelCase(f.getName(), '_').toUpperCase(Locale.ENGLISH));
98101
}
99102
},
100103

@@ -113,8 +116,9 @@ public String translateName(Field f) {
113116
*/
114117
LOWER_CASE_WITH_UNDERSCORES() {
115118
@Override
116-
public String translateName(Field f) {
117-
return separateCamelCase(f.getName(), '_').toLowerCase(Locale.ENGLISH);
119+
public List<String> translateName(Field f) {
120+
return Collections.singletonList(
121+
separateCamelCase(f.getName(), '_').toLowerCase(Locale.ENGLISH));
118122
}
119123
},
120124

@@ -140,8 +144,9 @@ public String translateName(Field f) {
140144
*/
141145
LOWER_CASE_WITH_DASHES() {
142146
@Override
143-
public String translateName(Field f) {
144-
return separateCamelCase(f.getName(), '-').toLowerCase(Locale.ENGLISH);
147+
public List<String> translateName(Field f) {
148+
return Collections.singletonList(
149+
separateCamelCase(f.getName(), '-').toLowerCase(Locale.ENGLISH));
145150
}
146151
},
147152

@@ -167,8 +172,9 @@ public String translateName(Field f) {
167172
*/
168173
LOWER_CASE_WITH_DOTS() {
169174
@Override
170-
public String translateName(Field f) {
171-
return separateCamelCase(f.getName(), '.').toLowerCase(Locale.ENGLISH);
175+
public List<String> translateName(Field f) {
176+
return Collections.singletonList(
177+
separateCamelCase(f.getName(), '.').toLowerCase(Locale.ENGLISH));
172178
}
173179
};
174180

gson/src/main/java/com/google/gson/FieldNamingStrategy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.gson;
1818

1919
import java.lang.reflect.Field;
20+
import java.util.List;
2021

2122
/**
2223
* A mechanism for providing custom field naming in Gson. This allows the client code to translate
@@ -33,8 +34,8 @@ public interface FieldNamingStrategy {
3334
* Translates the field name into its JSON field name representation.
3435
*
3536
* @param f the field object that we are translating
36-
* @return the translated field name.
37+
* @return the list of possible translated field names.
3738
* @since 1.3
3839
*/
39-
public String translateName(Field f);
40+
public List<String> translateName(Field f);
4041
}

gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ private boolean includeField(Field f, boolean serialize) {
8686
private List<String> getFieldNames(Field f) {
8787
SerializedName annotation = f.getAnnotation(SerializedName.class);
8888
if (annotation == null) {
89-
String name = fieldNamingPolicy.translateName(f);
90-
return Collections.singletonList(name);
89+
return fieldNamingPolicy.translateName(f);
9190
}
9291

9392
String serializedName = annotation.value();

gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Dummy {
103103
for (FieldNamingPolicy policy : policies) {
104104
// Should ignore default Locale
105105
assertWithMessage("Unexpected conversion for %s", policy)
106-
.that(policy.translateName(field))
106+
.that(policy.translateName(field).iterator().next())
107107
.matches(expected);
108108
}
109109
} finally {
@@ -142,7 +142,7 @@ class Dummy {
142142
for (FieldNamingPolicy policy : policies) {
143143
// Should ignore default Locale
144144
assertWithMessage("Unexpected conversion for %s", policy)
145-
.that(policy.translateName(field))
145+
.that(policy.translateName(field).iterator().next())
146146
.matches(expected);
147147
}
148148
} finally {

gson/src/test/java/com/google/gson/GsonBuilderTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.lang.reflect.Modifier;
2828
import java.lang.reflect.Type;
2929
import java.text.DateFormat;
30+
import java.util.Collections;
3031
import java.util.Date;
3132
import org.junit.Test;
3233

@@ -56,7 +57,7 @@ public void testCreatingMoreThanOnce() {
5657
assertThat(gson).isNotNull();
5758
assertThat(builder.create()).isNotNull();
5859

59-
builder.setFieldNamingStrategy(f -> "test");
60+
builder.setFieldNamingStrategy(f -> Collections.singletonList("test"));
6061

6162
Gson otherGson = builder.create();
6263
assertThat(otherGson).isNotNull();

gson/src/test/java/com/google/gson/GsonTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public final class GsonTest {
4747
private static final Excluder CUSTOM_EXCLUDER =
4848
Excluder.DEFAULT.excludeFieldsWithoutExposeAnnotation().disableInnerClassSerialization();
4949

50-
private static final FieldNamingStrategy CUSTOM_FIELD_NAMING_STRATEGY = f -> "foo";
50+
private static final FieldNamingStrategy CUSTOM_FIELD_NAMING_STRATEGY =
51+
f -> Collections.singletonList("foo");
5152

5253
private static final ToNumberStrategy CUSTOM_OBJECT_TO_NUMBER_STRATEGY = ToNumberPolicy.DOUBLE;
5354
private static final ToNumberStrategy CUSTOM_NUMBER_TO_NUMBER_STRATEGY =

gson/src/test/java/com/google/gson/functional/Java17RecordTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import com.google.gson.stream.JsonWriter;
4040
import java.io.IOException;
4141
import java.lang.reflect.Type;
42+
import java.util.Collections;
43+
4244
import org.junit.Test;
4345
import org.junit.runner.RunWith;
4446
import org.junit.runners.JUnit4;
@@ -106,7 +108,7 @@ public int i() {
106108
public void testFieldNamingStrategy() {
107109
record LocalRecord(int i) {}
108110

109-
Gson gson = new GsonBuilder().setFieldNamingStrategy(f -> f.getName() + "-custom").create();
111+
Gson gson = new GsonBuilder().setFieldNamingStrategy(f -> Collections.singletonList(f.getName() + "-custom")).create();
110112

111113
assertThat(gson.toJson(new LocalRecord(1))).isEqualTo("{\"i-custom\":1}");
112114
assertThat(gson.fromJson("{\"i-custom\":2}", LocalRecord.class)).isEqualTo(new LocalRecord(2));

gson/src/test/java/com/google/gson/functional/NamingPolicyTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.google.gson.common.TestTypes.ClassWithSerializedNameFields;
2727
import com.google.gson.common.TestTypes.StringWrapper;
2828
import java.lang.reflect.Field;
29+
import java.util.Collections;
30+
import java.util.List;
2931
import java.util.Locale;
3032
import org.junit.Before;
3133
import org.junit.Test;
@@ -156,7 +158,7 @@ public void testGsonDuplicateNameUsingSerializedNameFieldNamingPolicySerializati
156158

157159
@Test
158160
public void testGsonDuplicateNameDueToBadNamingPolicy() {
159-
Gson gson = builder.setFieldNamingStrategy(f -> "x").create();
161+
Gson gson = builder.setFieldNamingStrategy(f -> Collections.singletonList("x")).create();
160162

161163
var e =
162164
assertThrows(IllegalArgumentException.class, () -> gson.toJson(new ClassWithTwoFields()));
@@ -244,8 +246,8 @@ static final class AtName {
244246

245247
private static final class UpperCaseNamingStrategy implements FieldNamingStrategy {
246248
@Override
247-
public String translateName(Field f) {
248-
return f.getName().toUpperCase(Locale.ROOT);
249+
public List<String> translateName(Field f) {
250+
return Collections.singletonList(f.getName().toUpperCase(Locale.ROOT));
249251
}
250252
}
251253

0 commit comments

Comments
 (0)