Skip to content

Default values for primitive parameters #26

Closed
@michaelhixson

Description

@michaelhixson

I was surprised by the behavior of Jackson+Kotlin with respect to primitive parameters that have default values. It seems like those default values are ignored, and zero is used instead.

Here's an example:

class Main private constructor() {
  compainion object {
    @JvmStatic fun main(args: Array<String>) {
      val mapper = ObjectMapper().registerModule(KotlinModule())
      val string1 = "{\"i\":3}"
      val string2 = "{}"
      val value1 = mapper.readValue(string1, Foo::class.java)
      val value2 = mapper.readValue(string2, Foo::class.java)
      println("value1: $value1")
      println("value2: $value2")
    }
  }

  data class Foo
  @JsonCreator constructor(@JsonProperty val i: Int = 5)
}

That prints:

value1: Foo(i=3)
value2: Foo(i=0)

But I wanted it to print:

value1: Foo(i=3)
value2: Foo(i=5)

Is that beyond the scope of this module? Is it impossible for some reason?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions