io.gsonfire.util.FieldNameResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gson-fire Show documentation
Show all versions of gson-fire Show documentation
A java library that adds some very useful features to Gson, like Date serializing to unix timestamp or RFC3339, method (getter) serialization, pre and post processors and many more. Check out the documentation to learn how to use it!
package io.gsonfire.util;
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public final class FieldNameResolver {
private final FieldNamingStrategy fieldNamingStrategy;
private final ConcurrentMap fieldNameCache = new ConcurrentHashMap();
public FieldNameResolver(Gson gson) {
this.fieldNamingStrategy = getFieldNamingStrategy(gson);
}
public String getFieldName(final Field field) {
String fieldName = fieldNameCache.get(field);
if(fieldName == null){
SerializedName serializedName = field.getAnnotation(SerializedName.class);
if (serializedName == null) {
fieldName = fieldNamingStrategy.translateName(field);
} else {
fieldName = serializedName.value();
}
if(!fieldNameCache.containsKey(field)){
fieldNameCache.put(field, fieldName);
}
}
return fieldName;
}
private FieldNamingStrategy getFieldNamingStrategy(Gson gson) {
return gson.fieldNamingStrategy();
}
}