Example usage for com.google.gson FieldNamingStrategy translateName

List of usage examples for com.google.gson FieldNamingStrategy translateName

Introduction

In this page you can find the example usage for com.google.gson FieldNamingStrategy translateName.

Prototype

public String translateName(Field f);

Source Link

Document

Translates the field name into its JSON field name representation.

Usage

From source file:com.ryanharter.auto.value.gson.internal.Util.java

License:Apache License

public static Map<String, String> renameFields(Class<?> targetClass, List<String> names,
        FieldNamingStrategy fieldNamingStrategy) {
    Map<String, String> renamedFields = new LinkedHashMap<>();
    for (String fieldName : names) {
        if (fieldNamingStrategy instanceof FieldNamingPolicy) {
            switch ((FieldNamingPolicy) fieldNamingStrategy) {
            case UPPER_CAMEL_CASE:
                renamedFields.put(fieldName, upperCaseFirstLetter(fieldName));
                break;
            case UPPER_CAMEL_CASE_WITH_SPACES:
                renamedFields.put(fieldName, upperCaseFirstLetter(separateCamelCase(fieldName, " ")));
                break;
            case LOWER_CASE_WITH_UNDERSCORES:
                renamedFields.put(fieldName, separateCamelCase(fieldName, "_").toLowerCase(Locale.ENGLISH));
                break;
            case LOWER_CASE_WITH_DASHES:
                renamedFields.put(fieldName, separateCamelCase(fieldName, "-").toLowerCase(Locale.ENGLISH));
                break;
            case LOWER_CASE_WITH_DOTS:
                renamedFields.put(fieldName, separateCamelCase(fieldName, ".").toLowerCase(Locale.ENGLISH));
                break;
            default:
                renamedFields.put(fieldName, fieldName);
            }//w  ww.  ja  v  a 2 s . c  o m
        } else {
            try {
                renamedFields.put(fieldName,
                        fieldNamingStrategy.translateName(targetClass.getDeclaredField(fieldName)));
            } catch (NoSuchFieldException E) {
                renamedFields.put(fieldName, fieldName);
            }
        }
    }
    return renamedFields;
}