Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give FieldNamingStrategy the ability to return multiple String names #2776

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions gson/src/main/java/com/google/gson/FieldNamingStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.google.gson;

import com.google.gson.annotations.SerializedName;
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 @@ -37,4 +40,15 @@
* @since 1.3
*/
public String translateName(Field f);

/**
* Translates the field name into its JSON field alternative names representation. used for
mfriesen marked this conversation as resolved.
Show resolved Hide resolved
* deserialization only. This is similar to {@link SerializedName#alternate()}.
*
* @param f the field object that we are translating
* @return the list of possible translated field names.
*/
mfriesen marked this conversation as resolved.
Show resolved Hide resolved
default List<String> translateToAlternateNames(Field f) {
mfriesen marked this conversation as resolved.
Show resolved Hide resolved
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ 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> alternates = fieldNamingPolicy.translateToAlternateNames(f);
List<String> fieldNames = new ArrayList<>(alternates.size() + 1);
fieldNames.add(fieldName);
fieldNames.addAll(alternates);
return fieldNames;
}

String serializedName = annotation.value();
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,55 @@ 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");
mfriesen marked this conversation as resolved.
Show resolved Hide resolved
}

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

@Override
public String translateName(Field f) {
return "some-constant-string-instance-field";
}

@Override
public List<String> translateToAlternateNames(Field f) {
return List.of("SomeConstantStringInstanceField");
}
})
.create();
StringWrapper target = new StringWrapper("blah");
assertThat(gson.toJson(target))
.isEqualTo(
"{\"some-constant-string-instance-field\":\""
+ target.someConstantStringInstanceField
+ "\"}");
}

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