1 /* <lambda>null2 * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 @file:Suppress("EqualsOrHashCode") 5 6 package kotlinx.serialization.json 7 8 import kotlinx.serialization.Serializable 9 import kotlinx.serialization.Transient 10 import kotlinx.serialization.json.internal.* 11 import kotlinx.serialization.test.assertFailsWithSerial 12 import kotlin.test.* 13 14 class JsonTransientTest : JsonTestBase() { 15 16 @Serializable 17 class Data(val a: Int = 0, @Transient var b: Int = 42, val e: Boolean = false) { 18 var c = "Hello" 19 val d: String 20 get() = "hello" 21 22 override fun equals(other: Any?): Boolean { 23 if (this === other) return true 24 if (other == null || this::class != other::class) return false 25 26 other as Data 27 28 if (a != other.a) return false 29 if (b != other.b) return false 30 if (c != other.c) return false 31 if (d != other.d) return false 32 33 return true 34 } 35 36 override fun toString(): String { 37 return "Data(a=$a, b=$b, e=$e, c='$c', d='$d')" 38 } 39 } 40 41 @Test 42 fun testAll() = parametrizedTest { jsonTestingMode -> 43 assertEquals("""{"a":0,"e":false,"c":"Hello"}""", 44 default.encodeToString(Data.serializer(), Data(), jsonTestingMode)) 45 } 46 47 @Test 48 fun testMissingOptionals() = parametrizedTest { jsonTestingMode -> 49 assertEquals(default.decodeFromString(Data.serializer(), """{"a":0,"c":"Hello"}""", jsonTestingMode), Data()) 50 assertEquals(default.decodeFromString(Data.serializer(), """{"a":0}""", jsonTestingMode), Data()) 51 } 52 53 @Test 54 fun testThrowTransient() = parametrizedTest { jsonTestingMode -> 55 assertFailsWithSerial("JsonDecodingException") { 56 default.decodeFromString(Data.serializer(), """{"a":0,"b":100500,"c":"Hello"}""", jsonTestingMode) 57 } 58 } 59 } 60