Skip to content

Commit

Permalink
Give FieldNamingStrategy the ability to return multiple String names
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Nov 15, 2024
1 parent 78caa5e commit 3bc670e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 26 deletions.
34 changes: 20 additions & 14 deletions gson/src/main/java/com/google/gson/FieldNamingPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.google.gson;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
Expand All @@ -33,8 +35,8 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
/** Using this naming policy with Gson will ensure that the field name is unchanged. */
IDENTITY() {
@Override
public String translateName(Field f) {
return f.getName();
public List<String> translateName(Field f) {
return Collections.singletonList(f.getName());
}
},

Expand All @@ -51,8 +53,8 @@ public String translateName(Field f) {
*/
UPPER_CAMEL_CASE() {
@Override
public String translateName(Field f) {
return upperCaseFirstLetter(f.getName());
public List<String> translateName(Field f) {
return Collections.singletonList(upperCaseFirstLetter(f.getName()));
}
},

Expand All @@ -71,8 +73,8 @@ public String translateName(Field f) {
*/
UPPER_CAMEL_CASE_WITH_SPACES() {
@Override
public String translateName(Field f) {
return upperCaseFirstLetter(separateCamelCase(f.getName(), ' '));
public List<String> translateName(Field f) {
return Collections.singletonList(upperCaseFirstLetter(separateCamelCase(f.getName(), ' ')));
}
},

Expand All @@ -93,8 +95,9 @@ public String translateName(Field f) {
*/
UPPER_CASE_WITH_UNDERSCORES() {
@Override
public String translateName(Field f) {
return separateCamelCase(f.getName(), '_').toUpperCase(Locale.ENGLISH);
public List<String> translateName(Field f) {
return Collections.singletonList(
separateCamelCase(f.getName(), '_').toUpperCase(Locale.ENGLISH));
}
},

Expand All @@ -113,8 +116,9 @@ public String translateName(Field f) {
*/
LOWER_CASE_WITH_UNDERSCORES() {
@Override
public String translateName(Field f) {
return separateCamelCase(f.getName(), '_').toLowerCase(Locale.ENGLISH);
public List<String> translateName(Field f) {
return Collections.singletonList(
separateCamelCase(f.getName(), '_').toLowerCase(Locale.ENGLISH));
}
},

Expand All @@ -140,8 +144,9 @@ public String translateName(Field f) {
*/
LOWER_CASE_WITH_DASHES() {
@Override
public String translateName(Field f) {
return separateCamelCase(f.getName(), '-').toLowerCase(Locale.ENGLISH);
public List<String> translateName(Field f) {
return Collections.singletonList(
separateCamelCase(f.getName(), '-').toLowerCase(Locale.ENGLISH));
}
},

Expand All @@ -167,8 +172,9 @@ public String translateName(Field f) {
*/
LOWER_CASE_WITH_DOTS() {
@Override
public String translateName(Field f) {
return separateCamelCase(f.getName(), '.').toLowerCase(Locale.ENGLISH);
public List<String> translateName(Field f) {
return Collections.singletonList(
separateCamelCase(f.getName(), '.').toLowerCase(Locale.ENGLISH));
}
};

Expand Down
5 changes: 3 additions & 2 deletions gson/src/main/java/com/google/gson/FieldNamingStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.gson;

import java.lang.reflect.Field;
import java.util.List;

/**
* A mechanism for providing custom field naming in Gson. This allows the client code to translate
Expand All @@ -33,8 +34,8 @@ public interface FieldNamingStrategy {
* Translates the field name into its JSON field name representation.
*
* @param f the field object that we are translating
* @return the translated field name.
* @return the list of possible translated field names.
* @since 1.3
*/
public String translateName(Field f);
public List<String> translateName(Field f);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ private boolean includeField(Field f, boolean serialize) {
private List<String> getFieldNames(Field f) {
SerializedName annotation = f.getAnnotation(SerializedName.class);
if (annotation == null) {
String name = fieldNamingPolicy.translateName(f);
return Collections.singletonList(name);
return fieldNamingPolicy.translateName(f);
}

String serializedName = annotation.value();
Expand Down
4 changes: 2 additions & 2 deletions gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Dummy {
for (FieldNamingPolicy policy : policies) {
// Should ignore default Locale
assertWithMessage("Unexpected conversion for %s", policy)
.that(policy.translateName(field))
.that(policy.translateName(field).iterator().next())
.matches(expected);
}
} finally {
Expand Down Expand Up @@ -142,7 +142,7 @@ class Dummy {
for (FieldNamingPolicy policy : policies) {
// Should ignore default Locale
assertWithMessage("Unexpected conversion for %s", policy)
.that(policy.translateName(field))
.that(policy.translateName(field).iterator().next())
.matches(expected);
}
} finally {
Expand Down
3 changes: 2 additions & 1 deletion gson/src/test/java/com/google/gson/GsonBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.Collections;
import java.util.Date;
import org.junit.Test;

Expand Down Expand Up @@ -56,7 +57,7 @@ public void testCreatingMoreThanOnce() {
assertThat(gson).isNotNull();
assertThat(builder.create()).isNotNull();

builder.setFieldNamingStrategy(f -> "test");
builder.setFieldNamingStrategy(f -> Collections.singletonList("test"));

Gson otherGson = builder.create();
assertThat(otherGson).isNotNull();
Expand Down
3 changes: 2 additions & 1 deletion gson/src/test/java/com/google/gson/GsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public final class GsonTest {
private static final Excluder CUSTOM_EXCLUDER =
Excluder.DEFAULT.excludeFieldsWithoutExposeAnnotation().disableInnerClassSerialization();

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

private static final ToNumberStrategy CUSTOM_OBJECT_TO_NUMBER_STRATEGY = ToNumberPolicy.DOUBLE;
private static final ToNumberStrategy CUSTOM_NUMBER_TO_NUMBER_STRATEGY =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collections;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -106,7 +108,7 @@ public int i() {
public void testFieldNamingStrategy() {
record LocalRecord(int i) {}

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

assertThat(gson.toJson(new LocalRecord(1))).isEqualTo("{\"i-custom\":1}");
assertThat(gson.fromJson("{\"i-custom\":2}", LocalRecord.class)).isEqualTo(new LocalRecord(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.google.gson.common.TestTypes.ClassWithSerializedNameFields;
import com.google.gson.common.TestTypes.StringWrapper;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -156,7 +158,7 @@ public void testGsonDuplicateNameUsingSerializedNameFieldNamingPolicySerializati

@Test
public void testGsonDuplicateNameDueToBadNamingPolicy() {
Gson gson = builder.setFieldNamingStrategy(f -> "x").create();
Gson gson = builder.setFieldNamingStrategy(f -> Collections.singletonList("x")).create();

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

private static final class UpperCaseNamingStrategy implements FieldNamingStrategy {
@Override
public String translateName(Field f) {
return f.getName().toUpperCase(Locale.ROOT);
public List<String> translateName(Field f) {
return Collections.singletonList(f.getName().toUpperCase(Locale.ROOT));
}
}

Expand Down

0 comments on commit 3bc670e

Please sign in to comment.