Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
- Changed fieldName to be added first to the fieldName list
- test which verifies that when 'alternate names' are configured they don't affect serialization, and only affect deserialization
- Updated translateToAlternateNames JavaDoc to refer it works like SerializedName#alternate()
  • Loading branch information
mfriesen committed Nov 17, 2024
1 parent 4ebc2c7 commit 05a58e6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
6 changes: 4 additions & 2 deletions gson/src/main/java/com/google/gson/FieldNamingStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.gson;

import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
Expand All @@ -35,13 +36,14 @@ public interface FieldNamingStrategy {
* Translates the field name into its JSON field name representation.
*
* @param f the field object that we are translating
* @return the list of possible translated field names.
* @return the translated field name.
* @since 1.3
*/
public String translateName(Field f);

/**
* Translates the field name into its JSON field alternative names representation.
* Translates the field name into its JSON field alternative names representation. used for
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** Type adapter that reflects over the fields and methods of a class. */
public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory {
Expand Down Expand Up @@ -87,9 +89,9 @@ private List<String> getFieldNames(Field f) {
SerializedName annotation = f.getAnnotation(SerializedName.class);
if (annotation == null) {
String fieldName = fieldNamingPolicy.translateName(f);
List<String> fieldNames = new ArrayList<>(fieldNamingPolicy.translateToAlternateNames(f));
fieldNames.add(fieldName);
return fieldNames;
List<String> alternateNames = fieldNamingPolicy.translateToAlternateNames(f);
return Stream.concat(Stream.of(fieldName), alternateNames.stream())
.collect(Collectors.toList());
}

String serializedName = annotation.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,32 @@ public List<String> translateToAlternateNames(Field f) {
assertThat(deserializedObject.someConstantStringInstanceField).isEqualTo("someValue");
}

@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

0 comments on commit 05a58e6

Please sign in to comment.