1 /* 2 * Copyright 2018 JetBrains s.r.o. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package kotlinx.serialization 18 19 import kotlinx.serialization.json.* 20 import org.junit.* 21 import org.junit.Assert.* 22 23 class SerializationCasesTest : JsonTestBase() { 24 25 @Serializable 26 data class Data1(val a: Int, val b: Int) 27 28 @Serializer(forClass = Data1::class) 29 object ExtDataSerializer1 30 31 @Test testConstructorValPropertiesnull32 fun testConstructorValProperties() { 33 val data = Data1(1, 2) 34 35 // Serialize with internal serializer for Data class 36 assertEquals("""{"a":1,"b":2}""", default.encodeToString(data)) 37 assertEquals(data, Json.decodeFromString<Data1>("""{"a":1,"b":2}""")) 38 39 // Serialize with external serializer for Data class 40 assertEquals("""{"a":1,"b":2}""", default.encodeToString(ExtDataSerializer1, data)) 41 assertEquals(data, Json.decodeFromString(ExtDataSerializer1, """{"a":1,"b":2}""")) 42 } 43 44 @Serializable 45 class Data2 { 46 var a = 0 47 var b = 0 equalsnull48 override fun equals(other: Any?) = other is Data2 && other.a == a && other.b == b 49 } 50 51 @Serializer(forClass=Data2::class) 52 object ExtDataSerializer2 53 54 @Test 55 fun testBodyVarProperties() { 56 val data = Data2().apply { 57 a = 1 58 b = 2 59 } 60 61 // Serialize with internal serializer for Data class 62 assertEquals("""{"a":1,"b":2}""", default.encodeToString(data)) 63 assertEquals(data, Json.decodeFromString<Data2>("""{"a":1,"b":2}""")) 64 65 // Serialize with external serializer for Data class 66 assertEquals("""{"a":1,"b":2}""", default.encodeToString(ExtDataSerializer2, data)) 67 assertEquals(data, Json.decodeFromString(ExtDataSerializer2, """{"a":1,"b":2}""")) 68 } 69 70 enum class TintEnum { LIGHT, DARK } 71 72 @Serializable 73 data class Data3( 74 val a: String, 75 val b: List<Int>, 76 val c: Map<String, TintEnum> 77 ) 78 79 // Serialize with external serializer for Data class 80 @Serializer(forClass = Data3::class) 81 object ExtDataSerializer3 82 83 @Test testNestedValuesnull84 fun testNestedValues() { 85 val data = Data3("Str", listOf(1, 2), mapOf("lt" to TintEnum.LIGHT, "dk" to TintEnum.DARK)) 86 // Serialize with internal serializer for Data class 87 val expected = """{"a":"Str","b":[1,2],"c":{"lt":"LIGHT","dk":"DARK"}}""" 88 assertEquals(expected, default.encodeToString(data)) 89 assertEquals(data, Json.decodeFromString<Data3>(expected)) 90 assertEquals(expected, default.encodeToString(ExtDataSerializer3, data)) 91 assertEquals(data, Json.decodeFromString(ExtDataSerializer3, expected)) 92 } 93 } 94