summaryrefslogtreecommitdiff
path: root/core/commonMain/src/kotlinx/serialization/internal/ObjectSerializer.kt
blob: ac9ee8e3ac377b36278a6cace8de2cef8e6ed39b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.internal

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*

/**
 * Serializer for Kotlin's singletons (denoted by `object` keyword).
 * To preserve singleton identity after serialization and deserialization, object serializer
 * uses an [object instance][objectInstance].
 * By default, a singleton is serialized as an empty structure, e.g. `{}` in JSON.
 */
@PublishedApi
@OptIn(ExperimentalSerializationApi::class)
internal class ObjectSerializer<T : Any>(serialName: String, private val objectInstance: T) : KSerializer<T> {

    @PublishedApi // See comment in SealedClassSerializer
    internal constructor(
        serialName: String,
        objectInstance: T,
        classAnnotations: Array<Annotation>
    ) : this(serialName, objectInstance) {
        _annotations = classAnnotations.asList()
    }

    private var _annotations: List<Annotation> = emptyList()

    override val descriptor: SerialDescriptor by lazy(LazyThreadSafetyMode.PUBLICATION) {
        buildSerialDescriptor(serialName, StructureKind.OBJECT) {
            annotations = _annotations
        }
    }

    override fun serialize(encoder: Encoder, value: T) {
        encoder.beginStructure(descriptor).endStructure(descriptor)
    }

    override fun deserialize(decoder: Decoder): T {
        decoder.decodeStructure(descriptor) {
            if (decodeSequentially())
                return@decodeStructure

            when (val index = decodeElementIndex(descriptor)) {
                CompositeDecoder.DECODE_DONE -> {
                    return@decodeStructure
                }
                else -> throw SerializationException("Unexpected index $index")
            }
        }
        return objectInstance
    }
}