Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Convert to custom class using GSON and Generics with Kotlin in XPLPC project

Posted on November 11, 2022 By

Solution 1 :

The problem is that for your decodeFunctionReturnValue function the type parameter T is not reified. Therefore at runtime the TypeToken you are creating is actually TypeToken<JsonFunctionReturnValueData<Object>>. When deserializing Object, Gson creates based on the JSON data standard objects, for a JSON object that is a Java Map (and Gson’s internal implementation is LinkedTreeMap).

So if possible the solution would be to make the type parameter T here reified as well. If that is not possible, maybe the type can be passed as separate TypeToken parameter to the function.


As side note: It was already suggested that Gson should throw an exception when a TypeToken capturing a type variable is created (which would have informed you about the problem with your code). However, due to backward compatibility concerns this was not implemented yet.

Problem :

I have a problem that appear be simple, but im making something wrong.

How i can convert from a generic class with reified using GSON and Kotlin in my project called XPLPC?

The error is here:
https://github.com/xplpc/xplpc/actions/runs/3395808126/jobs/5646151499#step:9:503

com.xplpc.library.TodoTest > singleItem[test(AVD) - 12] FAILED 
    java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.xplpc.library.TodoTest$Todo
    at com.xplpc.library.TodoTest.singleItem(TodoTest.kt:44)

It don’t understand that i want convert to Todo class instead of LinkedTreeMap.

The code is here:
https://github.com/xplpc/xplpc/blob/main/kotlin/lib/library/src/main/java/com/xplpc/serializer/JsonSerializer.kt#L66-L79

And the code is called by this function:
https://github.com/xplpc/xplpc/blob/main/kotlin/lib/library/src/main/java/com/xplpc/client/RemoteClient.kt#L14-L27

Basically the code is:

// Part 1
object RemoteClient {
    inline fun <reified T> call(request: Request, defValue: T? = null): T? {
        try {
            val data = PlatformProxy.call(request.data)
            println(data)
            return XPLPC.config.serializer.decodeFunctionReturnValue<T>(data)
        } catch (e: Exception) {
            Log.e(
                Constants.LOG_GROUP,
                "[RemoteClient : call] Error when try to decode return value: ${e.message}"
            )
        }

        return defValue
    }

    inline fun <reified T> callAsync(request: Request, defValue: T? = null): Deferred<T?> {
        return CoroutineScope(Dispatchers.IO).async {
            [email protected] call<T>(request, defValue)
        }
    }
}

// Part 2
override fun <T> decodeFunctionReturnValue(data: String): T? {
    try {
        val type = object : TypeToken<JsonFunctionReturnValueData<T>>() {}.type
        val gson = createGson()
        return gson.fromJson<JsonFunctionReturnValueData<T>>(data, type).r
    } catch (e: Exception) {
        Log.e(
            Constants.LOG_GROUP,
            "[JsonSerializer : decodeFunctionReturnValue] Error when parse json: ${e.message}"
        )
    }

    return null
}

Comments

Comment posted by Paulo Coutinho

Nice. It can be reified because JsonSerializer inherit from an interface, and interface cant have reified. How i can pass TypeToken as parameter, since JsonFunctionReturnValueData is a class available only for JsonSerializer. Basically, how to make JsonFunctionReturnValueData works with generic and Type?

READ  [FIXED] java - Android how to start animations in series
Powered by Inline Related Posts

Comment posted by TypeToken.getParameterized

@PauloCoutinho, if

Comment posted by github.com/xplpc/xplpc/blob/main/kotlin/lib/library/src/main/…

Thanks. It works. Can you check if it is correct or can be problematic in future? Only a review:

Comment posted by Marcono1234

@PauloCoutinho, I only had a short look at it, but to me that looks good.

Android Tags:android, gson, java, json, kotlin

Post navigation

Previous Post: Do you have to pay for MyNetDiary?
Next Post: [FIXED] Simple Cucumber Test is not running on Sample Android Project

Related Posts

[FIXED] android – Why Firebase App Check fails in app release but not in debugging mode? Android
[FIXED] Gradle: Could not resolve com.squareup.okhttp3:okhttp:3.12.+ Android
[FIXED] kotlin – Can I still delete sms from android inbox in the latest version? Android
[FIXED] Fragment getting initialised before Android 12 Splash screen ends Android
[FIXED] android – How to Sign Out from Firebase Auth? Android
[FIXED] How to make String between characters to * like te***t in android? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme