forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonBsonDecoder.kt
154 lines (141 loc) · 5.96 KB
/
JsonBsonDecoder.kt
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
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bson.codecs.kotlinx
import java.util.Base64
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.modules.SerializersModule
import org.bson.AbstractBsonReader
import org.bson.BsonBinarySubType
import org.bson.BsonType
import org.bson.UuidRepresentation
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.toJsonNamingStrategy
import org.bson.internal.UuidHelper
@OptIn(ExperimentalSerializationApi::class)
internal interface JsonBsonDecoder : BsonDecoder, JsonDecoder {
val reader: AbstractBsonReader
val configuration: BsonConfiguration
fun json(): Json = Json {
explicitNulls = configuration.explicitNulls
encodeDefaults = configuration.encodeDefaults
classDiscriminator = configuration.classDiscriminator
namingStrategy = configuration.bsonNamingStrategy.toJsonNamingStrategy()
serializersModule = [email protected]
}
@Suppress("ComplexMethod")
override fun decodeJsonElement(): JsonElement =
reader.run {
when (currentBsonType) {
BsonType.DOCUMENT -> readJsonObject()
BsonType.ARRAY -> readJsonArray()
BsonType.NULL -> JsonPrimitive(decodeNull())
BsonType.STRING -> JsonPrimitive(decodeString())
BsonType.BOOLEAN -> JsonPrimitive(decodeBoolean())
BsonType.INT32 -> JsonPrimitive(decodeInt())
BsonType.INT64 -> JsonPrimitive(decodeLong())
BsonType.DOUBLE -> JsonPrimitive(decodeDouble())
BsonType.DECIMAL128 -> JsonPrimitive(reader.readDecimal128())
BsonType.OBJECT_ID -> JsonPrimitive(decodeObjectId().toHexString())
BsonType.DATE_TIME -> JsonPrimitive(reader.readDateTime())
BsonType.TIMESTAMP -> JsonPrimitive(reader.readTimestamp().value)
BsonType.BINARY -> {
val subtype = reader.peekBinarySubType()
val data = reader.readBinaryData().data
when (subtype) {
BsonBinarySubType.UUID_LEGACY.value ->
JsonPrimitive(
UuidHelper.decodeBinaryToUuid(data, subtype, UuidRepresentation.JAVA_LEGACY).toString())
BsonBinarySubType.UUID_STANDARD.value ->
JsonPrimitive(
UuidHelper.decodeBinaryToUuid(data, subtype, UuidRepresentation.STANDARD).toString())
else -> JsonPrimitive(Base64.getEncoder().encodeToString(data))
}
}
else -> error("Unsupported json type: $currentBsonType")
}
}
private fun readJsonObject(): JsonObject {
reader.readStartDocument()
val obj = buildJsonObject {
var type = reader.readBsonType()
while (type != BsonType.END_OF_DOCUMENT) {
put(reader.readName(), decodeJsonElement())
type = reader.readBsonType()
}
}
reader.readEndDocument()
return obj
}
private fun readJsonArray(): JsonArray {
reader.readStartArray()
val array = buildJsonArray {
var type = reader.readBsonType()
while (type != BsonType.END_OF_DOCUMENT) {
add(decodeJsonElement())
type = reader.readBsonType()
}
}
reader.readEndArray()
return array
}
}
internal class JsonBsonDecoderImpl(
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
) : BsonDecoderImpl(reader, serializersModule, configuration), JsonBsonDecoder {
override val json = json()
}
internal class JsonBsonArrayDecoder(
descriptor: SerialDescriptor,
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
) : BsonArrayDecoder(descriptor, reader, serializersModule, configuration), JsonBsonDecoder {
override val json = json()
}
internal class JsonBsonDocumentDecoder(
descriptor: SerialDescriptor,
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
) : BsonDocumentDecoder(descriptor, reader, serializersModule, configuration), JsonBsonDecoder {
override val json = json()
}
internal class JsonBsonPolymorphicDecoder(
descriptor: SerialDescriptor,
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
) : BsonPolymorphicDecoder(descriptor, reader, serializersModule, configuration), JsonBsonDecoder {
override val json = json()
}
internal class JsonBsonMapDecoder(
descriptor: SerialDescriptor,
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
) : BsonMapDecoder(descriptor, reader, serializersModule, configuration), JsonBsonDecoder {
override val json = json()
}