Skip to content

Strange error "Could not find creator property with name" with multiple constructors #231

@OndrejSpanel

Description

@OndrejSpanel

When I run the code below with Jackson 2.6.3, I get an error during deserialization:

import com.fasterxml.jackson.annotation._
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper

object Main extends App {

  // uncommenting following line makes error disappear - understandable workaround
  //@JsonIgnoreProperties(Array("dummy"))
  class Assembly(val script: String, dummy: Int) {
    @JsonCreator
    def this(name: Int)(script: String) = {
      this(script, 0)
    }

    // uncommenting following line makes error disappear - strange
    //def this(script: String) = this(script, 0)
  }

  val jacksonMapper = new ObjectMapper() with ScalaObjectMapper

  jacksonMapper.registerModule(new DefaultScalaModule)

  val toSave = new Assembly(0)("script")

  val out = jacksonMapper.writeValueAsString(toSave)
  println(out)

  val check = jacksonMapper.readValue(out, classOf[Assembly])
  println(check)
}

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Could not find creator property with name 'dummy' (in class Main$Assembly)
at [Source: {"script":"script"}; line: 1, column: 1]

It is possible to use JsonIgnoreProperties as a workaround.

It seems the Jackson is somehow confused the by the constructors and is listing dummy as a property. Adding another constructor makes the error go away.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions