1 /* 2 * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.serialization 6 7 import kotlinx.serialization.json.* 8 import kotlinx.serialization.modules.* 9 import kotlin.native.concurrent.* 10 11 @Serializable 12 open class PolyBase(val id: Int) { hashCodenull13 override fun hashCode(): Int { 14 return id 15 } 16 toStringnull17 override fun toString(): String { 18 return "PolyBase(id=$id)" 19 } 20 equalsnull21 override fun equals(other: Any?): Boolean { 22 if (this === other) return true 23 if (other == null || this::class != other::class) return false 24 other as PolyBase 25 if (id != other.id) return false 26 return true 27 } 28 29 } 30 31 // TODO sandwwraith moving this class to the corresponding tests breaks runtime in unexpected ways 32 @Serializable 33 data class PolyDefault(val json: JsonElement) : PolyBase(-1) 34 35 class PolyDefaultWithId(id: Int) : PolyBase(id) 36 37 @Serializable 38 data class PolyDerived(val s: String) : PolyBase(1) 39 <lambda>null40val BaseAndDerivedModule = SerializersModule { 41 polymorphic(PolyBase::class, PolyBase.serializer()) { 42 subclass(PolyDerived.serializer()) 43 } 44 } 45