It is safe to use KClass<T>.serializer() even if is internal?
See original GitHub issueI’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:
- Created 3 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @mltbnz and @Baileypollard, I’ve updated the project with the new memory model and new dependencies and everything is working for me!
@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.