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

gsonpath.model.SerializedNameFetcher.kt Maven / Gradle / Ivy

Go to download

An annotation processor which generates Type Adapters for the Google Gson library

There is a newer version: 4.0.0
Show newest version
package gsonpath.model

import com.google.gson.annotations.SerializedName
import gsonpath.NestedJson
import gsonpath.ProcessingException

object SerializedNameFetcher {
    fun getSerializedName(fieldInfo: FieldInfo, flattenDelimiter: Char): String? {
        val serializedNameAnnotation = fieldInfo.getAnnotation(SerializedName::class.java)
        val nestedJson = fieldInfo.getAnnotation(NestedJson::class.java)

        // SerializedName 'alternate' is not supported and should fail fast.
        serializedNameAnnotation?.let {
            if (it.alternate.isNotEmpty()) {
                throw ProcessingException("SerializedName 'alternate' feature is not supported", fieldInfo.element)
            }
        }

        nestedJson?.let {
            if (it.value.endsWith(flattenDelimiter)) {
                throw ProcessingException("NestedJson path must not end with '$flattenDelimiter'", fieldInfo.element)
            }
        }

        return when {
            nestedJson != null && serializedNameAnnotation != null -> {
                nestedJson.value + flattenDelimiter + serializedNameAnnotation.value
            }
            nestedJson != null -> nestedJson.value + flattenDelimiter
            serializedNameAnnotation != null -> serializedNameAnnotation.value
            else -> null
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy