Skip to content

Commit 817845e

Browse files
committed
JsonBsonEncoder: fix handling of negative values
Also fixes formatting in KotlinSerializerCodecTest.kt.
1 parent bcb30a2 commit 817845e

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import kotlinx.serialization.json.JsonNull
2626
import kotlinx.serialization.json.JsonObject
2727
import kotlinx.serialization.json.JsonPrimitive
2828
import kotlinx.serialization.json.double
29-
import kotlinx.serialization.json.int
30-
import kotlinx.serialization.json.long
3129
import kotlinx.serialization.modules.SerializersModule
3230
import org.bson.BsonWriter
3331
import org.bson.codecs.kotlinx.utils.BsonCodecUtils.toJsonNamingStrategy
@@ -41,7 +39,6 @@ internal class JsonBsonEncoder(
4139
) : BsonEncoderImpl(writer, serializersModule, configuration), JsonEncoder {
4240

4341
companion object {
44-
private val DOUBLE_MIN_VALUE = BigDecimal.valueOf(Double.MIN_VALUE)
4542
private val DOUBLE_MAX_VALUE = BigDecimal.valueOf(Double.MAX_VALUE)
4643
private val INT_MIN_VALUE = BigDecimal.valueOf(Int.MIN_VALUE.toLong())
4744
private val INT_MAX_VALUE = BigDecimal.valueOf(Int.MAX_VALUE.toLong())
@@ -104,7 +101,7 @@ internal class JsonBsonEncoder(
104101
val decimal = BigDecimal(content).stripTrailingZeros()
105102
when {
106103
decimal.scale() > 0 ->
107-
if (DOUBLE_MIN_VALUE <= decimal && decimal <= DOUBLE_MAX_VALUE) {
104+
if (decimal.abs() <= DOUBLE_MAX_VALUE) {
108105
encodeDouble(primitive.double)
109106
} else {
110107
writer.writeDecimal128(Decimal128(decimal))

bson-kotlinx/src/test/kotlin/org/bson/codecs/kotlinx/KotlinSerializerCodecTest.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ import org.bson.codecs.kotlinx.samples.SealedInterface
117117
import org.bson.codecs.kotlinx.samples.ValueClass
118118
import org.bson.json.JsonMode
119119
import org.bson.json.JsonWriterSettings
120-
import org.bson.types.Decimal128
121120
import org.junit.jupiter.api.Test
122121
import org.junit.jupiter.api.assertThrows
123122
import org.junit.jupiter.params.ParameterizedTest
@@ -224,18 +223,27 @@ class KotlinSerializerCodecTest {
224223
@JvmStatic
225224
fun testJsonPrimitiveNumberEncoding(): Stream<Pair<String, String>> {
226225
return Stream.of(
227-
"""{"value": 0}""" to """{"value": 0}""",
226+
"""{"value": 0}""" to """{"value": 0}""",
228227
"""{"value": 0}""" to """{"value": 0.0}""",
229-
"""{"value": 1.1}""" to """{"value": 1.1E0}""",
230-
"""{"value": 11}""" to """{"value": 1.1E1}""",
231-
"""{"value": 110}""" to """{"value": 1.1E2}""",
232-
"""{"value": 1100}""" to """{"value": 1.1E3}""",
233-
"""{"value": 0.1}""" to """{"value": 1E-1}""",
234-
"""{"value": 0.01}""" to """{"value": 1E-2}""",
235-
"""{"value": 0.001}""" to """{"value": 1E-3}""",
236-
"""{"value": 35485464}""" to """{"value": 35485464}""",
237-
"""{"value": 35485464}""" to """{"value": 35485464.0}""",
238-
"""{"value": {"${'$'}numberDecimal": "123456789123456789123456789"}}""" to """{"value": 123456789123456789123456789}"""
228+
"""{"value": 1.1}""" to """{"value": 1.1E0}""",
229+
"""{"value": 11}""" to """{"value": 1.1E1}""",
230+
"""{"value": 110}""" to """{"value": 1.1E2}""",
231+
"""{"value": 1100}""" to """{"value": 1.1E3}""",
232+
"""{"value": 0.1}""" to """{"value": 1E-1}""",
233+
"""{"value": 0.01}""" to """{"value": 1E-2}""",
234+
"""{"value": 0.001}""" to """{"value": 1E-3}""",
235+
"""{"value": -1.1}""" to """{"value": -1.1E0}""",
236+
"""{"value": -11}""" to """{"value": -1.1E1}""",
237+
"""{"value": -110}""" to """{"value": -1.1E2}""",
238+
"""{"value": -1100}""" to """{"value": -1.1E3}""",
239+
"""{"value": -0.1}""" to """{"value": -1E-1}""",
240+
"""{"value": -0.01}""" to """{"value": -1E-2}""",
241+
"""{"value": -0.001}""" to """{"value": -1E-3}""",
242+
"""{"value": 9223372036854775807}""" to """{"value": 9223372036854775807}""",
243+
"""{"value": {"${'$'}numberDecimal": "9223372036854775808"}}""" to """{"value": 9223372036854775808}""",
244+
"""{"value": -9223372036854775808}""" to """{"value": -9223372036854775808}""",
245+
"""{"value": {"${'$'}numberDecimal": "-9223372036854775809"}}""" to
246+
"""{"value": -9223372036854775809}""",
239247
)
240248
}
241249
}

0 commit comments

Comments
 (0)