summaryrefslogtreecommitdiff
path: root/runtime/commonMain/src/kotlinx/serialization/json/internal/TreeJsonInput.kt
blob: 55785b5c60505cad86525d5c4a902ae9ec477c58 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
@file:Suppress("LeakingThis")

package kotlinx.serialization.json.internal

import kotlinx.serialization.*
import kotlinx.serialization.internal.*
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlin.jvm.*

internal fun <T> Json.readJson(element: JsonElement, deserializer: DeserializationStrategy<T>): T {
    val descriptor = deserializer.descriptor
    if (element is JsonNull) {
        // TODO temporary workaround (?)
        require(descriptor.isNullable) { "Read JsonNull and expected nullable descriptor, but has $descriptor" }
        @Suppress("NULL_FOR_NONNULL_TYPE")
        return null
    }

    val input = when (descriptor.kind) {
        StructureKind.LIST -> JsonTreeListInput(this, cast(element))
        StructureKind.MAP -> JsonTreeMapInput(this, cast(element))
        is PrimitiveKind -> JsonPrimitiveInput(this, cast(element))
        else -> JsonTreeInput(this, cast(element))
    }

    return input.decode(deserializer)
}

private sealed class AbstractJsonTreeInput(override val json: Json, open val obj: JsonElement)
    : NamedValueDecoder(), JsonInput {

    override val context: SerialModule
        get() = json.context

    @JvmField
    protected val configuration = json.configuration

    private fun currentObject() = currentTagOrNull?.let { currentElement(it) } ?: obj

    override fun decodeJson(): JsonElement = currentObject()

    @Suppress("DEPRECATION")
    override val updateMode: UpdateMode
        get() = configuration.updateMode

    override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
        return decodeSerializableValuePolymorphic(deserializer)
    }

    override fun composeName(parentName: String, childName: String): String = childName

    override fun beginStructure(desc: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeDecoder {
        val currentObject = currentObject()
        return when (desc.kind) {
            StructureKind.LIST -> JsonTreeListInput(json, cast(currentObject))
            StructureKind.MAP -> JsonTreeMapInput(json, cast(currentObject))
            else -> JsonTreeInput(json, cast(currentObject))
        }
    }

    protected open fun getValue(tag: String): JsonPrimitive {
        val currentElement = currentElement(tag)
        return currentElement as? JsonPrimitive ?: throw JsonElementTypeMismatchException("$currentElement at $tag", "JsonPrimitive")
    }

    protected abstract fun currentElement(tag: String): JsonElement

    override fun decodeTaggedChar(tag: String): Char {
        val o = getValue(tag)
        return if (o.content.length == 1) o.content[0] else throw SerializationException("$o can't be represented as Char")
    }

    override fun decodeTaggedEnum(tag: String, enumDescription: EnumDescriptor): Int =
        enumDescription.getElementIndexOrThrow(getValue(tag).content)

    override fun decodeTaggedNull(tag: String): Nothing? = null

    override fun decodeTaggedNotNullMark(tag: String): Boolean = currentElement(tag) !== JsonNull

    override fun decodeTaggedUnit(tag: String) {
        return
    }

    override fun decodeTaggedBoolean(tag: String): Boolean = getValue(tag).boolean
    override fun decodeTaggedByte(tag: String): Byte = getValue(tag).int.toByte()
    override fun decodeTaggedShort(tag: String) = getValue(tag).int.toShort()
    override fun decodeTaggedInt(tag: String) = getValue(tag).int
    override fun decodeTaggedLong(tag: String) = getValue(tag).long
    override fun decodeTaggedFloat(tag: String) = getValue(tag).float
    override fun decodeTaggedDouble(tag: String) = getValue(tag).double
    override fun decodeTaggedString(tag: String) = getValue(tag).content
}

private class JsonPrimitiveInput(json: Json, override val obj: JsonPrimitive) : AbstractJsonTreeInput(json, obj) {

    companion object {
        const val primitive = "primitive"
    }

    init {
        pushTag(primitive)
    }

    override fun currentElement(tag: String): JsonElement {
        require(tag == primitive)
        return obj
    }
}

private open class JsonTreeInput(json: Json, override val obj: JsonObject) : AbstractJsonTreeInput(json, obj) {
    private var position = 0

    override fun decodeElementIndex(desc: SerialDescriptor): Int {
        while (position < desc.elementsCount) {
            val name = desc.getTag(position++)
            if (name in obj) {
                return position - 1
            }
        }
        return CompositeDecoder.READ_DONE
    }

    override fun currentElement(tag: String): JsonElement = obj.getValue(tag)

    override fun endStructure(desc: SerialDescriptor) {
        if (!configuration.strictMode || desc is PolymorphicClassDescriptor) return

        // Validate keys
        val names = HashSet<String>(desc.elementsCount)
        for (i in 0 until desc.elementsCount) {
            names += desc.getElementName(i)
        }

        for (key in obj.keys) {
            if (key !in names) throw JsonUnknownKeyException("Encountered an unknown key '$key'")
        }
    }
}

private class JsonTreeMapInput(json: Json, override val obj: JsonObject) : JsonTreeInput(json, obj) {
    private val keys = obj.keys.toList()
    private val size: Int = keys.size * 2
    private var position = -1

    override fun elementName(desc: SerialDescriptor, index: Int): String {
        val i = index / 2
        return keys[i]
    }

    override fun decodeElementIndex(desc: SerialDescriptor): Int {
        while (position < size - 1) {
            position++
            return position
        }
        return CompositeDecoder.READ_DONE
    }

    override fun currentElement(tag: String): JsonElement {
        return if (position % 2 == 0) JsonLiteral(tag) else obj.getValue(tag)
    }

    override fun endStructure(desc: SerialDescriptor) {
        // do nothing, maps do not have strict keys, so strict mode check is omitted
    }
}

private class JsonTreeListInput(json: Json, override val obj: JsonArray) : AbstractJsonTreeInput(json, obj) {
    private val size = obj.content.size
    private var currentIndex = -1

    override fun elementName(desc: SerialDescriptor, index: Int): String = (index).toString()

    override fun currentElement(tag: String): JsonElement {
        return obj[tag.toInt()]
    }

    override fun decodeElementIndex(desc: SerialDescriptor): Int {
        while (currentIndex < size - 1) {
            currentIndex++
            return currentIndex
        }
        return CompositeDecoder.READ_DONE
    }
}