0

Hi I have this code in Kotlin Android I try to parse JSON string to data clas, but the data is unknown, I mean the data class is genric type T

class JsonFeatureFlag<T>(
        key: String,
        @StringRes nameResId: Int,
        category: DevOptionCategory,
        val defaultValue: T
    ): FeatureFlagV2<T>(key, nameResId, category) {
    .....
    override fun getValue(): T {
            val json = FirebaseRemoteConfig.getInstance().getString(key)
            return try {
                Gson().fromJson(json, object: TypeToken<T>(){}.type)
            } catch (e: Exception) {
                reportInvalidJson(json)
                defaultValue
            }
        }
    ....
    }


And I'm getting this error: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to X

While I understand that TypeToken should be used for generic, something isn't working.

5
  • 1
    Due to type erasure at runtime the T is just java.lang.Object (respectively Kotlin's Any, I assume). Gson deserializes Object by default to a Map. If you can change your JsonFeatureFlag class to include the runtime class of T, then you can directly pass that class to the Gson.fromJson call. Commented Jan 29, 2022 at 22:46
  • 1
    Does this answer your question? Gson, ClassCastException with Generics Commented Jan 29, 2022 at 22:51
  • thanks @Marcono1234 , I also read regarding the type erasure. I have change it to Gson().fromJson(json, className) and I passing the className: Class<T> to the method Commented Jan 30, 2022 at 9:53
  • 1
    If that solved your issue, would you mind adding your own answer to this StackOverflow question to indicate that it has been answered? Commented Jan 30, 2022 at 19:44
  • hi @Marcono1234 I sow this answer stackoverflow.com/questions/25431859/… but it does not answer my question. I do not have a list or arrylist. My T generic can be any class Commented Jan 31, 2022 at 11:59

1 Answer 1

0

To solve this problem I provide a class name i.e. className: Class Although I used genric, when somebody wants to use my API and call getValue the type is known, and he can provide the class name to the get value method.

class JsonFeatureFlag<T>(
    key: String,
    @StringRes nameResId: Int,
    category: DevOptionCategory,
    val defaultValue: T
): FeatureFlagV2<T>(key, nameResId, category) {


     fun getValue(className: Class<T>): T {
        val json = FirebaseRemoteConfig.getInstance().getString(key)
        return try {
            Gson().fromJson(json, className)
        } catch (e: Exception) {
            reportInvalidJson(json)
            defaultValue
        }
    }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.