All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.gsonfire.util.FieldNameResolver Maven / Gradle / Ivy

Go to download

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!

There is a newer version: 1.9.0
Show newest version
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();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy