1 /* 2 * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 // TODO: Move to common tests after https://youtrack.jetbrains.com/issue/KT-28927 is fixed 6 7 @file:UseContextualSerialization(Int::class, IntHolder::class) 8 9 package kotlinx.serialization.features 10 11 import kotlinx.serialization.* 12 import kotlinx.serialization.json.* 13 import kotlinx.serialization.modules.* 14 import kotlin.test.* 15 16 @Serializable 17 data class Carrier3( 18 val a: IntHolder, 19 val i: Int, 20 val nullable: Int?, 21 val nullableIntHolder: IntHolder?, 22 val nullableIntList: List<Int?> = emptyList(), 23 val nullableIntHolderNullableList: List<IntHolder?>? = null 24 ) 25 26 class ContextualSerializationOnFileTest { <lambda>null27 val module = SerializersModule { 28 contextual(DividingIntSerializer) 29 contextual(MultiplyingIntHolderSerializer) 30 } <lambda>null31 val json = Json { serializersModule = module; encodeDefaults = true } 32 33 @Test testOnFilenull34 fun testOnFile() { 35 val str = json.encodeToString(Carrier3.serializer(), Carrier3(IntHolder(42), 8, 8, IntHolder(42))) 36 assertEquals( 37 """{"a":84,"i":4,"nullable":4,"nullableIntHolder":84,"nullableIntList":[],"nullableIntHolderNullableList":null}""", 38 str 39 ) 40 } 41 } 42