Skip to content

Serializer coerce string to int Β #1042

@yackermann

Description

@yackermann

Describe the bug

When requiring type of Int in a serializable class, type coerce string to int without throwing an error.

To Reproduce
Attach a code snippet or test data if possible.

import kotlinx.serialization.*
import kotlinx.serialization.json.*

@Serializable 
class Person(val age: Int)

fun main() {
    val obj = Json.decodeFromString<Person>("{\"age\": \"22\"}")
    println(obj) // Person(age=22)
}

Expected behavior

EXCEPTION

Environment

  • Kotlin version: [e.g. 1.4.0-RC]
  • Library version: [e.g. 1.0.0-RC]
  • Kotlin platforms: Multiplatform
  • Gradle version: [e.g. 6.5.1]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions