@@ -25,6 +25,7 @@ internal open class StreamingJsonDecoder(
25
25
descriptor : SerialDescriptor ,
26
26
discriminatorHolder : DiscriminatorHolder ?
27
27
) : JsonDecoder, ChunkedDecoder, AbstractDecoder() {
28
+ private val coercePrimitives = json.configuration.allowPrimitiveCoercion
28
29
29
30
// A mutable reference to the discriminator that have to be skipped when in optimistic phase
30
31
// of polymorphic serialization, see `decodeSerializableValue`
@@ -273,47 +274,51 @@ internal open class StreamingJsonDecoder(
273
274
}
274
275
275
276
/*
276
- * The primitives are allowed to be quoted and unquoted
277
- * to simplify map key parsing and integrations with third-party API.
278
- */
277
+ * The primitives are allowed to be quoted and unquoted
278
+ * to simplify map key parsing and integrations with third-party API.
279
+ */
279
280
override fun decodeBoolean (): Boolean {
280
- return lexer.consumeBooleanLenient()
281
+ return if (coercePrimitives) {
282
+ lexer.consumeBooleanLenient()
283
+ } else {
284
+ lexer.consumeBoolean()
285
+ }
281
286
}
282
287
283
288
override fun decodeByte (): Byte {
284
- val value = lexer.consumeNumericLiteral()
289
+ val value = lexer.consumeNumericLiteral(coercePrimitives )
285
290
// Check for overflow
286
291
if (value != value.toByte().toLong()) lexer.fail(" Failed to parse byte for input '$value '" )
287
292
return value.toByte()
288
293
}
289
294
290
295
override fun decodeShort (): Short {
291
- val value = lexer.consumeNumericLiteral()
296
+ val value = lexer.consumeNumericLiteral(coercePrimitives )
292
297
// Check for overflow
293
298
if (value != value.toShort().toLong()) lexer.fail(" Failed to parse short for input '$value '" )
294
299
return value.toShort()
295
300
}
296
301
297
302
override fun decodeInt (): Int {
298
- val value = lexer.consumeNumericLiteral()
303
+ val value = lexer.consumeNumericLiteral(coercePrimitives )
299
304
// Check for overflow
300
305
if (value != value.toInt().toLong()) lexer.fail(" Failed to parse int for input '$value '" )
301
306
return value.toInt()
302
307
}
303
308
304
309
override fun decodeLong (): Long {
305
- return lexer.consumeNumericLiteral()
310
+ return lexer.consumeNumericLiteral(coercePrimitives )
306
311
}
307
312
308
313
override fun decodeFloat (): Float {
309
- val result = lexer.parseString(" float" ) { toFloat() }
314
+ val result = lexer.parseString(" float" , coercePrimitives ) { toFloat() }
310
315
val specialFp = json.configuration.allowSpecialFloatingPointValues
311
316
if (specialFp || result.isFinite()) return result
312
317
lexer.throwInvalidFloatingPointDecoded(result)
313
318
}
314
319
315
320
override fun decodeDouble (): Double {
316
- val result = lexer.parseString(" double" ) { toDouble() }
321
+ val result = lexer.parseString(" double" , coercePrimitives ) { toDouble() }
317
322
val specialFp = json.configuration.allowSpecialFloatingPointValues
318
323
if (specialFp || result.isFinite()) return result
319
324
lexer.throwInvalidFloatingPointDecoded(result)
@@ -374,15 +379,16 @@ internal class JsonDecoderForUnsignedTypes(
374
379
) : AbstractDecoder() {
375
380
override val serializersModule: SerializersModule = json.serializersModule
376
381
override fun decodeElementIndex (descriptor : SerialDescriptor ): Int = error(" unsupported" )
382
+ private val coercePrimitves = json.configuration.allowPrimitiveCoercion
377
383
378
- override fun decodeInt (): Int = lexer.parseString(" UInt" ) { toUInt().toInt() }
379
- override fun decodeLong (): Long = lexer.parseString(" ULong" ) { toULong().toLong() }
380
- override fun decodeByte (): Byte = lexer.parseString(" UByte" ) { toUByte().toByte() }
381
- override fun decodeShort (): Short = lexer.parseString(" UShort" ) { toUShort().toShort() }
384
+ override fun decodeInt (): Int = lexer.parseString(" UInt" , coercePrimitves ) { toUInt().toInt() }
385
+ override fun decodeLong (): Long = lexer.parseString(" ULong" , coercePrimitves ) { toULong().toLong() }
386
+ override fun decodeByte (): Byte = lexer.parseString(" UByte" , coercePrimitves ) { toUByte().toByte() }
387
+ override fun decodeShort (): Short = lexer.parseString(" UShort" , coercePrimitves ) { toUShort().toShort() }
382
388
}
383
389
384
- private inline fun <T > AbstractJsonLexer.parseString (expectedType : String , block : String .() -> T ): T {
385
- val input = consumeStringLenient( )
390
+ private inline fun <T > AbstractJsonLexer.parseString (expectedType : String , coercePrimitives : Boolean , block : String .() -> T ): T {
391
+ val input = consumeOther(allowQuoted = coercePrimitives )
386
392
try {
387
393
return input.block()
388
394
} catch (e: IllegalArgumentException ) {
0 commit comments