@@ -26,6 +26,7 @@ import kotlinx.datetime.LocalTime
2626import kotlinx.serialization.ExperimentalSerializationApi
2727import kotlinx.serialization.MissingFieldException
2828import kotlinx.serialization.SerializationException
29+ import kotlinx.serialization.json.Json
2930import kotlinx.serialization.json.JsonPrimitive
3031import kotlinx.serialization.json.buildJsonArray
3132import kotlinx.serialization.json.buildJsonObject
@@ -146,10 +147,10 @@ class KotlinSerializerCodecTest {
146147 | "code": {"${' $' } code": "int i = 0;"},
147148 | "codeWithScope": {"${' $' } code": "int x = y", "${' $' } scope": {"y": 1}},
148149 | "dateTime": {"${' $' } date": {"${' $' } numberLong": "1577836801000"}},
149- | "decimal128": {"${' $' } numberDecimal": "1.0 "},
150+ | "decimal128": {"${' $' } numberDecimal": "1.1 "},
150151 | "documentEmpty": {},
151152 | "document": {"a": {"${' $' } numberInt": "1"}},
152- | "double": {"${' $' } numberDouble": "62.0 "},
153+ | "double": {"${' $' } numberDouble": "62.1 "},
153154 | "int32": {"${' $' } numberInt": "42"},
154155 | "int64": {"${' $' } numberLong": "52"},
155156 | "maxKey": {"${' $' } maxKey": 1},
@@ -218,6 +219,35 @@ class KotlinSerializerCodecTest {
218219 .append(" boolean" , BsonBoolean .TRUE )
219220 .append(" string" , BsonString (" String" )))
220221 }
222+
223+ @JvmStatic
224+ fun testJsonPrimitiveNumberEncoding (): Stream <Pair <String , String >> {
225+ return Stream .of(
226+ """ {"value": 0}""" to """ {"value": 0}""" ,
227+ """ {"value": 0}""" to """ {"value": 0.0}""" ,
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}""" ,
247+ """ {"value": {"${' $' } numberDecimal": "1.8E+309"}}""" to """ {"value": 1.8E+309}""" ,
248+ """ {"value": {"${' $' } numberDecimal": "1E-325"}}""" to """ {"value": 1E-325}""" ,
249+ )
250+ }
221251 }
222252
223253 @ParameterizedTest
@@ -832,9 +862,9 @@ class KotlinSerializerCodecTest {
832862 |"short": 1,
833863 |"int": 22,
834864 |"long": {"$numberLong ": "3000000000"},
835- |"decimal": {"$numberDecimal ": "10000000000000000000 "}
836- |"decimal2": {"$numberDecimal ": "3.1230E +700"}
837- |"float": 4.0 ,
865+ |"decimal": {"$numberDecimal ": "1E+19 "}
866+ |"decimal2": {"$numberDecimal ": "3.123E +700"}
867+ |"float": 4.1 ,
838868 |"double": 4.2,
839869 |"boolean": true,
840870 |"string": "String"
@@ -849,9 +879,9 @@ class KotlinSerializerCodecTest {
849879 put(" short" , 1 )
850880 put(" int" , 22 )
851881 put(" long" , 3_000_000_000 )
852- put(" decimal" , BigDecimal (" 10000000000000000000 " ))
853- put(" decimal2" , BigDecimal (" 3.1230E +700" ))
854- put(" float" , 4.0 )
882+ put(" decimal" , BigDecimal (" 1E+19 " ))
883+ put(" decimal2" , BigDecimal (" 3.123E +700" ))
884+ put(" float" , 4.1 )
855885 put(" double" , 4.2 )
856886 put(" boolean" , true )
857887 put(" string" , " String" )
@@ -1023,10 +1053,10 @@ class KotlinSerializerCodecTest {
10231053 put(" binary" , JsonPrimitive (" S2Fma2Egcm9ja3Mh" ))
10241054 put(" boolean" , JsonPrimitive (true ))
10251055 put(" dateTime" , JsonPrimitive (1577836801000 ))
1026- put(" decimal128" , JsonPrimitive (1.0 ))
1056+ put(" decimal128" , JsonPrimitive (1.1 ))
10271057 put(" documentEmpty" , buildJsonObject {})
10281058 put(" document" , buildJsonObject { put(" a" , JsonPrimitive (1 )) })
1029- put(" double" , JsonPrimitive (62.0 ))
1059+ put(" double" , JsonPrimitive (62.1 ))
10301060 put(" int32" , JsonPrimitive (42 ))
10311061 put(" int64" , JsonPrimitive (52 ))
10321062 put(" objectId" , JsonPrimitive (" 211111111111111111111112" ))
@@ -1050,6 +1080,13 @@ class KotlinSerializerCodecTest {
10501080 assertDecodesTo(""" {"value": $jsonAllSupportedTypesDocument }""" , dataClassWithAllSupportedJsonTypes)
10511081 }
10521082
1083+ @ParameterizedTest
1084+ @MethodSource(" testJsonPrimitiveNumberEncoding" )
1085+ fun testJsonPrimitiveNumberEncoding (test : Pair <String , String >) {
1086+ val (expected, actual) = test
1087+ assertEncodesTo(expected, Json .parseToJsonElement(actual))
1088+ }
1089+
10531090 @Test
10541091 fun testDataFailures () {
10551092 assertThrows<MissingFieldException >(" Missing data" ) {
0 commit comments