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.

Regression when serialize wrapped Any type property annotated with @Contextual

See original GitHub issue

Describe the bug Kotlin version: 1.4.0 + kotlinx-serialization-core:1.0.0-RC) + @Contextual val data: Any? reports:

Exception in thread “main” kotlinx.serialization.SerializationException: Serializer for class ‘Any’ is not found.

But when (Kotlin version: 1.3.70 + kotlinx-serialization-runtime:0.20.0), @ContextualSerialization val data: Any? works fine.

To Reproduce

@Serializable
abstract class Box(
        var code: String,
        var msg: String?
){
    constructor(): this(OK, null)
    companion object{
        const val OK = "OK"
    }
}

@Serializable
//data class DataBox(@ContextualSerialization val data: Any?) : Box(OK,null)  //kotlin 1.3.70
data class DataBox(@Contextual val data: Any?) : Box(OK,null) //kotlin 1.4.0
{
    constructor(): this(null)
}

fun test3(){
    val json = Json{}
    //val str = json.stringify(DataBox.serializer(),DataBox(data = Person("Tom"))) //kotlin 1.3.70, ouput: {"code":"OK","msg":null,"data":{"name":"Tom"}}
    val str = json.encodeToString(DataBox.serializer(),DataBox(data = Person("Tom")))// kotlin 1.4.0: Execption!!! 
    println(str)
}

reports error:

Exception in thread “main” kotlinx.serialization.SerializationException: Serializer for class ‘Any’ is not found. Mark the class as @Serializable or provide the serializer explicitly. at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:127) at kotlinx.serialization.ContextualSerializer.serialize(ContextualSerializer.kt:52) at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:223) at kotlinx.serialization.encoding.Encoder$DefaultImpls.encodeNullableSerializableValue(Encoding.kt:296) at kotlinx.serialization.json.JsonEncoder$DefaultImpls.encodeNullableSerializableValue(JsonEncoder.kt) at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeNullableSerializableValue(StreamingJsonEncoder.kt:15) at kotlinx.serialization.encoding.AbstractEncoder.encodeNullableSerializableElement(AbstractEncoder.kt:94) at DataBox.write$Self(main.kt) at DataBox$$serializer.serialize(main.kt) at DataBox$$serializer.serialize(main.kt:32) at kotlinx.serialization.json.internal.StreamingJsonEncoder.encodeSerializableValue(StreamingJsonEncoder.kt:223) at kotlinx.serialization.json.Json.encodeToString(Json.kt:73) at MainKt.test3(main.kt:138)

Expected behavior Expect the behavior of 1.4.0 + 1.0.0-RC same as one of 1.3.70 + 0.20.0

Environment

  • Kotlin version: 1.4.0
  • Library version: 1.0.0-RC
  • Kotlin platforms: JVM
  • Gradle version: 6.3
  • IDE version: IntellijIDEA 2020.2.1 Other relevant context : MacOS 10.12.6, JDK 1.8.0_261

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
kikothemastercommented, Aug 31, 2020

I’m using Map<String, Any> for the feedback purposes where attributes could be any. But in the new version, I cannot use that anymore 👎

3reactions
VincentJoshuaETcommented, Sep 30, 2020

How do I use either Contextual or Polymorphic for Map<String, Any?>?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to (De)serialize field from object based on annotation ...
If the field is a collection of objects, serialize an array of id. Annotated fields get their property names serialized differently: If the ......
Read more >
Jackson-js: Powerful JavaScript decorators to serialize ...
This information is used during serialization and, more important, during deserialization to know about the type of a property/parameter.
Read more >
Contextual Bandits — Contextual Bandits documentation
Contents: Contextual Bandits; Installation; Getting started; Serializing (pickling) objects. Online Contextual Bandits.
Read more >
Chapter 4. Text Vectorization and Transformation Pipelines
Selection from Applied Text Analysis with Python [Book] ... Each property of the vector representation is a feature. For text, features represent attributes ......
Read more >
Serialized Form - Adobe Developer
Contains the web app context and the path, but no query parameters. ... deserializers (when no type information is needed for base type),...
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