question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It is safe to use KClass<T>.serializer() even if is internal?

See original GitHub issue

I’m working on a Multiplatform project and I’m building a custom serializer for Alamofire to deserialize a json to a Kotlin Class. That’s because I have all the DTOs on KMP side.

The way I found to achieve this is by using the KClass<T>.serializer() method that is marked as InternalSerializationApi. So I was wondering if it is safe to use or there is another way that I don’t know.

object JsonDecoder {
    @InternalSerializationApi
    fun decodeFromString(jsonString: String, objCClass: ObjCClass): Any {
        val kClazz = getOriginalKotlinClass(objCClass)!!
        val serializer = kClazz.serializer()
        return Json.decodeFromString(serializer, jsonString)
    }
}

Swift Code:

struct Serializer<T>: ResponseSerializer {
    func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> T {
        do {
            let x = try JsonDecoder().decodeFromString(jsonString: "{ }", objCClass: T.self as! AnyClass) as! T
            return x
        } catch {
            throw AFError.responseSerializationFailed(reason: .decodingFailed(error: error))
        }
    }
}

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
prof18commented, Nov 13, 2022

Hi @mltbnz and @Baileypollard, I’ve updated the project with the new memory model and new dependencies and everything is working for me!

0reactions
mltbnzcommented, Aug 31, 2022

@prof18 I am running into Terminating app due to uncaught exception 'NSGenericException', reason: 'Class BaseResponseDTO is abstract and can't be instantiated' on the Swift side when using your solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

It is safe to use KClass<T>.serializer() even if is internal?
I'm working on a Multiplatform project and I'm building a custom serializer for Alamofire to deserialize a json to a Kotlin Class.
Read more >
Does Kotlin can serialize class reference information?
Ok custom serializers seem to work here @Serializable data class SocketDto( @Serializable(with = ClassRefSerializer::class) val eventType: ...
Read more >
Serialization Interface — ItsDangerous Documentation (2.1.x)
This internal serializer can be changed by subclassing. To record and validate the age of the signature, see Signing With Timestamps. To serialize...
Read more >
Kafka Streams Data Types and Serialization
Every Kafka Streams application must provide Serdes (Serializer/Deserializer) for the data types of record keys and record values (e.g. java.lang.String or Avro ...
Read more >
Serialization overview in Orleans | Microsoft Learn
Learn about serialization and custom serializers in . ... to the same object still point to the same object after deserialization as well....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found