Skip to content

Commit a5dc456

Browse files
authored
Revert making BsonEncoder / BsonDecoder internal (#1510)
Made BsonEncoder / Decoder internal as part of the JsonElement support, however, this reduces the flexibility of the API and that change should be reverted. JAVA-5623
1 parent c717171 commit a5dc456

File tree

4 files changed

+135
-98
lines changed

4 files changed

+135
-98
lines changed

bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/BsonDecoder.kt

+9-71
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ import org.bson.BsonType
3737
import org.bson.BsonValue
3838
import org.bson.codecs.BsonValueCodec
3939
import org.bson.codecs.DecoderContext
40-
import org.bson.codecs.kotlinx.BsonDecoder.Companion.createBsonArrayDecoder
41-
import org.bson.codecs.kotlinx.BsonDecoder.Companion.createBsonDocumentDecoder
42-
import org.bson.codecs.kotlinx.BsonDecoder.Companion.createBsonMapDecoder
43-
import org.bson.codecs.kotlinx.BsonDecoder.Companion.createBsonPolymorphicDecoder
40+
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.createBsonArrayDecoder
41+
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.createBsonDecoder
42+
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.createBsonDocumentDecoder
43+
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.createBsonMapDecoder
44+
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.createBsonPolymorphicDecoder
4445
import org.bson.internal.NumberCodecHelper
4546
import org.bson.internal.StringCodecHelper
4647
import org.bson.types.ObjectId
@@ -51,75 +52,12 @@ import org.bson.types.ObjectId
5152
* For custom serialization handlers
5253
*/
5354
@ExperimentalSerializationApi
54-
internal sealed interface BsonDecoder : Decoder, CompositeDecoder {
55-
56-
/** Factory helper for creating concrete BsonDecoder implementations */
57-
companion object {
58-
59-
@Suppress("SwallowedException")
60-
private val hasJsonDecoder: Boolean by lazy {
61-
try {
62-
Class.forName("kotlinx.serialization.json.JsonDecoder")
63-
true
64-
} catch (e: ClassNotFoundException) {
65-
false
66-
}
67-
}
68-
69-
fun createBsonDecoder(
70-
reader: AbstractBsonReader,
71-
serializersModule: SerializersModule,
72-
configuration: BsonConfiguration
73-
): BsonDecoder {
74-
return if (hasJsonDecoder) JsonBsonDecoderImpl(reader, serializersModule, configuration)
75-
else BsonDecoderImpl(reader, serializersModule, configuration)
76-
}
77-
78-
fun createBsonArrayDecoder(
79-
descriptor: SerialDescriptor,
80-
reader: AbstractBsonReader,
81-
serializersModule: SerializersModule,
82-
configuration: BsonConfiguration
83-
): BsonArrayDecoder {
84-
return if (hasJsonDecoder) JsonBsonArrayDecoder(descriptor, reader, serializersModule, configuration)
85-
else BsonArrayDecoder(descriptor, reader, serializersModule, configuration)
86-
}
87-
88-
fun createBsonDocumentDecoder(
89-
descriptor: SerialDescriptor,
90-
reader: AbstractBsonReader,
91-
serializersModule: SerializersModule,
92-
configuration: BsonConfiguration
93-
): BsonDocumentDecoder {
94-
return if (hasJsonDecoder) JsonBsonDocumentDecoder(descriptor, reader, serializersModule, configuration)
95-
else BsonDocumentDecoder(descriptor, reader, serializersModule, configuration)
96-
}
97-
98-
fun createBsonPolymorphicDecoder(
99-
descriptor: SerialDescriptor,
100-
reader: AbstractBsonReader,
101-
serializersModule: SerializersModule,
102-
configuration: BsonConfiguration
103-
): BsonPolymorphicDecoder {
104-
return if (hasJsonDecoder) JsonBsonPolymorphicDecoder(descriptor, reader, serializersModule, configuration)
105-
else BsonPolymorphicDecoder(descriptor, reader, serializersModule, configuration)
106-
}
107-
108-
fun createBsonMapDecoder(
109-
descriptor: SerialDescriptor,
110-
reader: AbstractBsonReader,
111-
serializersModule: SerializersModule,
112-
configuration: BsonConfiguration
113-
): BsonMapDecoder {
114-
return if (hasJsonDecoder) JsonBsonMapDecoder(descriptor, reader, serializersModule, configuration)
115-
else BsonMapDecoder(descriptor, reader, serializersModule, configuration)
116-
}
117-
}
55+
public sealed interface BsonDecoder : Decoder, CompositeDecoder {
11856

11957
/** @return the decoded ObjectId */
120-
fun decodeObjectId(): ObjectId
58+
public fun decodeObjectId(): ObjectId
12159
/** @return the decoded BsonValue */
122-
fun decodeBsonValue(): BsonValue
60+
public fun decodeBsonValue(): BsonValue
12361
}
12462

12563
@OptIn(ExperimentalSerializationApi::class)
@@ -325,7 +263,7 @@ internal open class BsonPolymorphicDecoder(
325263
it.reset()
326264
mark = null
327265
}
328-
return deserializer.deserialize(BsonDecoder.createBsonDecoder(reader, serializersModule, configuration))
266+
return deserializer.deserialize(createBsonDecoder(reader, serializersModule, configuration))
329267
}
330268

331269
override fun decodeElementIndex(descriptor: SerialDescriptor): Int {

bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/BsonEncoder.kt

+3-25
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,21 @@ import org.bson.types.ObjectId
3939
* For custom serialization handlers
4040
*/
4141
@ExperimentalSerializationApi
42-
internal sealed interface BsonEncoder : Encoder, CompositeEncoder {
43-
44-
/** Factory helper for creating concrete BsonEncoder implementations */
45-
companion object {
46-
@Suppress("SwallowedException")
47-
private val hasJsonEncoder: Boolean by lazy {
48-
try {
49-
Class.forName("kotlinx.serialization.json.JsonEncoder")
50-
true
51-
} catch (e: ClassNotFoundException) {
52-
false
53-
}
54-
}
55-
56-
fun createBsonEncoder(
57-
writer: BsonWriter,
58-
serializersModule: SerializersModule,
59-
configuration: BsonConfiguration
60-
): BsonEncoder {
61-
return if (hasJsonEncoder) JsonBsonEncoder(writer, serializersModule, configuration)
62-
else BsonEncoderImpl(writer, serializersModule, configuration)
63-
}
64-
}
42+
public sealed interface BsonEncoder : Encoder, CompositeEncoder {
6543

6644
/**
6745
* Encodes an ObjectId
6846
*
6947
* @param value the ObjectId
7048
*/
71-
fun encodeObjectId(value: ObjectId)
49+
public fun encodeObjectId(value: ObjectId)
7250

7351
/**
7452
* Encodes a BsonValue
7553
*
7654
* @param value the BsonValue
7755
*/
78-
fun encodeBsonValue(value: BsonValue)
56+
public fun encodeBsonValue(value: BsonValue)
7957
}
8058

8159
/**

bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/KotlinSerializerCodec.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import org.bson.codecs.Codec
3434
import org.bson.codecs.DecoderContext
3535
import org.bson.codecs.EncoderContext
3636
import org.bson.codecs.configuration.CodecConfigurationException
37+
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.createBsonDecoder
38+
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.createBsonEncoder
3739
import org.bson.codecs.pojo.annotations.BsonCreator
3840
import org.bson.codecs.pojo.annotations.BsonDiscriminator
3941
import org.bson.codecs.pojo.annotations.BsonExtraElements
@@ -172,13 +174,13 @@ private constructor(
172174
}
173175

174176
override fun encode(writer: BsonWriter, value: T, encoderContext: EncoderContext) {
175-
serializer.serialize(BsonEncoder.createBsonEncoder(writer, serializersModule, bsonConfiguration), value)
177+
serializer.serialize(createBsonEncoder(writer, serializersModule, bsonConfiguration), value)
176178
}
177179

178180
override fun getEncoderClass(): Class<T> = kClass.java
179181

180182
override fun decode(reader: BsonReader, decoderContext: DecoderContext): T {
181183
require(reader is AbstractBsonReader)
182-
return serializer.deserialize(BsonDecoder.createBsonDecoder(reader, serializersModule, bsonConfiguration))
184+
return serializer.deserialize(createBsonDecoder(reader, serializersModule, bsonConfiguration))
183185
}
184186
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
package org.bson.codecs.kotlinx.utils
17+
18+
import kotlinx.serialization.ExperimentalSerializationApi
19+
import kotlinx.serialization.descriptors.SerialDescriptor
20+
import kotlinx.serialization.modules.SerializersModule
21+
import org.bson.AbstractBsonReader
22+
import org.bson.BsonWriter
23+
import org.bson.codecs.kotlinx.BsonArrayDecoder
24+
import org.bson.codecs.kotlinx.BsonConfiguration
25+
import org.bson.codecs.kotlinx.BsonDecoder
26+
import org.bson.codecs.kotlinx.BsonDecoderImpl
27+
import org.bson.codecs.kotlinx.BsonDocumentDecoder
28+
import org.bson.codecs.kotlinx.BsonEncoder
29+
import org.bson.codecs.kotlinx.BsonEncoderImpl
30+
import org.bson.codecs.kotlinx.BsonMapDecoder
31+
import org.bson.codecs.kotlinx.BsonPolymorphicDecoder
32+
import org.bson.codecs.kotlinx.JsonBsonArrayDecoder
33+
import org.bson.codecs.kotlinx.JsonBsonDecoderImpl
34+
import org.bson.codecs.kotlinx.JsonBsonDocumentDecoder
35+
import org.bson.codecs.kotlinx.JsonBsonEncoder
36+
import org.bson.codecs.kotlinx.JsonBsonMapDecoder
37+
import org.bson.codecs.kotlinx.JsonBsonPolymorphicDecoder
38+
39+
@ExperimentalSerializationApi
40+
internal object BsonCodecUtils {
41+
42+
@Suppress("SwallowedException")
43+
private val hasJsonEncoder: Boolean by lazy {
44+
try {
45+
Class.forName("kotlinx.serialization.json.JsonEncoder")
46+
true
47+
} catch (e: ClassNotFoundException) {
48+
false
49+
}
50+
}
51+
52+
@Suppress("SwallowedException")
53+
private val hasJsonDecoder: Boolean by lazy {
54+
try {
55+
Class.forName("kotlinx.serialization.json.JsonDecoder")
56+
true
57+
} catch (e: ClassNotFoundException) {
58+
false
59+
}
60+
}
61+
62+
internal fun createBsonEncoder(
63+
writer: BsonWriter,
64+
serializersModule: SerializersModule,
65+
configuration: BsonConfiguration
66+
): BsonEncoder {
67+
return if (hasJsonEncoder) JsonBsonEncoder(writer, serializersModule, configuration)
68+
else BsonEncoderImpl(writer, serializersModule, configuration)
69+
}
70+
71+
internal fun createBsonDecoder(
72+
reader: AbstractBsonReader,
73+
serializersModule: SerializersModule,
74+
configuration: BsonConfiguration
75+
): BsonDecoder {
76+
return if (hasJsonDecoder) JsonBsonDecoderImpl(reader, serializersModule, configuration)
77+
else BsonDecoderImpl(reader, serializersModule, configuration)
78+
}
79+
80+
internal fun createBsonArrayDecoder(
81+
descriptor: SerialDescriptor,
82+
reader: AbstractBsonReader,
83+
serializersModule: SerializersModule,
84+
configuration: BsonConfiguration
85+
): BsonArrayDecoder {
86+
return if (hasJsonDecoder) JsonBsonArrayDecoder(descriptor, reader, serializersModule, configuration)
87+
else BsonArrayDecoder(descriptor, reader, serializersModule, configuration)
88+
}
89+
90+
internal fun createBsonDocumentDecoder(
91+
descriptor: SerialDescriptor,
92+
reader: AbstractBsonReader,
93+
serializersModule: SerializersModule,
94+
configuration: BsonConfiguration
95+
): BsonDocumentDecoder {
96+
return if (hasJsonDecoder) JsonBsonDocumentDecoder(descriptor, reader, serializersModule, configuration)
97+
else BsonDocumentDecoder(descriptor, reader, serializersModule, configuration)
98+
}
99+
100+
internal fun createBsonPolymorphicDecoder(
101+
descriptor: SerialDescriptor,
102+
reader: AbstractBsonReader,
103+
serializersModule: SerializersModule,
104+
configuration: BsonConfiguration
105+
): BsonPolymorphicDecoder {
106+
return if (hasJsonDecoder) JsonBsonPolymorphicDecoder(descriptor, reader, serializersModule, configuration)
107+
else BsonPolymorphicDecoder(descriptor, reader, serializersModule, configuration)
108+
}
109+
110+
internal fun createBsonMapDecoder(
111+
descriptor: SerialDescriptor,
112+
reader: AbstractBsonReader,
113+
serializersModule: SerializersModule,
114+
configuration: BsonConfiguration
115+
): BsonMapDecoder {
116+
return if (hasJsonDecoder) JsonBsonMapDecoder(descriptor, reader, serializersModule, configuration)
117+
else BsonMapDecoder(descriptor, reader, serializersModule, configuration)
118+
}
119+
}

0 commit comments

Comments
 (0)