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 17, 2024
1 parent 78caa5e commit 0d769c7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
14 changes: 13 additions & 1 deletion gson/src/main/java/com/google/gson/FieldNamingStrategy.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;

/**
* A mechanism for providing custom field naming in Gson. This allows the client code to translate
Expand All @@ -33,8 +35,18 @@ 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);

/**
* Translates the field name into its JSON field alternative names representation.
*
* @param f the field object that we are translating
* @return the list of possible translated field names.
*/
default List<String> translateToAlternateNames(Field f) {

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'f' is never used.
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ 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);
String fieldName = fieldNamingPolicy.translateName(f);
List<String> fieldNames = new ArrayList<>(fieldNamingPolicy.translateToAlternateNames(f));
fieldNames.add(fieldName);
return fieldNames;
}

String serializedName = annotation.value();
Expand Down
6 changes: 2 additions & 4 deletions gson/src/test/java/com/google/gson/FieldNamingPolicyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ class Dummy {
for (FieldNamingPolicy policy : policies) {
// Should ignore default Locale
assertWithMessage("Unexpected conversion for %s", policy)
.that(policy.translateName(field))
.matches(expected);
.that(policy.translateName(field)).matches(expected);
}
} finally {
Locale.setDefault(oldLocale);
Expand Down Expand Up @@ -142,8 +141,7 @@ class Dummy {
for (FieldNamingPolicy policy : policies) {
// Should ignore default Locale
assertWithMessage("Unexpected conversion for %s", policy)
.that(policy.translateName(field))
.matches(expected);
.that(policy.translateName(field)).matches(expected);
}
} finally {
Locale.setDefault(oldLocale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.gson.common.TestTypes.ClassWithSerializedNameFields;
import com.google.gson.common.TestTypes.StringWrapper;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -237,6 +238,25 @@ public void testAtSignInSerializedName() {
assertThat(new Gson().toJson(new AtName())).isEqualTo("{\"@foo\":\"bar\"}");
}

@Test
public void testGsonWithAlternateNamesDeserialiation() {
Gson gson = builder.setFieldNamingStrategy(new FieldNamingStrategy() {

@Override
public String translateName(Field f) {
return "badname";
}

@Override
public List<String> translateToAlternateNames(Field f) {
return List.of("SomeConstantStringInstanceField");
}
}).create();
String target = "{\"SomeConstantStringInstanceField\":\"someValue\"}";
StringWrapper deserializedObject = gson.fromJson(target, StringWrapper.class);
assertThat(deserializedObject.someConstantStringInstanceField).isEqualTo("someValue");
}

static final class AtName {
@SerializedName("@foo")
String f = "bar";
Expand Down

0 comments on commit 0d769c7

Please sign in to comment.