-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't fail if there is no serializer for type parameters of contextua…
…l serializer Return null instead. Such behaviour is needed to support cachedChildSerializers logic. Since this field creator doesn't provide genericGetter (because it's static), type param serializer can't be retrieved, and the whole contextual serializer shouldn't be cached. #KT-58067 Fixed (cherry picked from commit fa8f38c)
- Loading branch information
1 parent
a75271c
commit 34efee5
Showing
4 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
plugins/kotlinx-serialization/testData/boxIr/contextualWithTypeParameters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// TARGET_BACKEND: JVM_IR | ||
|
||
// WITH_STDLIB | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.modules.* | ||
import kotlinx.serialization.encoding.* | ||
|
||
class SomeData<T>(val t: T) | ||
|
||
@Serializable | ||
class PagedData<T>( | ||
@Contextual val someData: SomeData<T>, | ||
) | ||
|
||
class SomeDataSerializer<T>(val tSer: KSerializer<T>) : KSerializer<SomeData<T>> { | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("SomeData") | ||
|
||
override fun serialize(encoder: Encoder, value: SomeData<T>) { | ||
encoder as JsonEncoder | ||
val data = encoder.json.encodeToJsonElement(tSer, value.t) | ||
val obj = buildJsonObject { | ||
put("innerType", tSer.descriptor.serialName) | ||
put("data", data) | ||
} | ||
encoder.encodeJsonElement(obj) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): SomeData<T> { | ||
TODO("Not yet implemented") | ||
} | ||
} | ||
|
||
fun box(): String { | ||
val module = SerializersModule { | ||
contextual(SomeData::class) { args -> SomeDataSerializer(args[0]) } | ||
} | ||
val json = Json { serializersModule = module } | ||
val input = PagedData<String>(SomeData("foo_bar")) | ||
val enc = json.encodeToString(input) | ||
return if (enc != """{"someData":{"innerType":"kotlin.String","data":"foo_bar"}}""") enc else "OK" | ||
} |
6 changes: 6 additions & 0 deletions
6
...en/org/jetbrains/kotlinx/serialization/runners/SerializationFirBlackBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...ests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.