• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package kotlinx.serialization.features
6 
7 import kotlinx.serialization.*
8 import kotlinx.serialization.encoding.*
9 import kotlinx.serialization.json.Json
10 import kotlin.test.Test
11 import kotlin.test.assertEquals
12 
13 @Serializable(WithNull.Companion::class)
14 data class WithNull(@SerialName("value") val nullable: String? = null) {
15     @Serializer(forClass = WithNull::class)
16     companion object : KSerializer<WithNull> {
serializenull17         override fun serialize(encoder: Encoder, value: WithNull) {
18             val elemOutput = encoder.beginStructure(descriptor)
19             if (value.nullable != null) elemOutput.encodeStringElement(descriptor, 0, value.nullable)
20             elemOutput.endStructure(descriptor)
21         }
22     }
23 }
24 
25 class PartiallyCustomSerializerTest {
26     @Test
partiallyCustomnull27     fun partiallyCustom() {
28         assertEquals("""{"value":"foo"}""", Json.encodeToString(WithNull.serializer(), WithNull("foo")))
29         assertEquals("""{}""", Json.encodeToString(WithNull.serializer(), WithNull()))
30     }
31 }
32