Skip to content

Deserialization with @JsonAnySetter #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
migel opened this issue Mar 28, 2015 · 1 comment
Open

Deserialization with @JsonAnySetter #193

migel opened this issue Mar 28, 2015 · 1 comment

Comments

@migel
Copy link

migel commented Mar 28, 2015

Hi, I get an exception when using @JsonAnySetter:

com.fasterxml.jackson.databind.JsonMappingException: Argument #0 of constructor [constructor for java.util.LinkedHashMap, annotations: [null]] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator

The test case:

import org.scalatest.FunSuite
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonAnySetter
import java.util.HashMap
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility

class Data3 {
  @JsonProperty var a = 3;

  @JsonAnyGetter def getProperties() = {
    val map = new HashMap[String, Object]()
    map.put("b", 2.asInstanceOf[Object])
    map
  }

  @JsonAnySetter def setProperties(name: String, value: Object) {
    println(s"name: ${name} class: ${value.getClass.getName} value: ${value}")
  }
}

class TestAnyGetterSetter extends FunSuite {
  test("anygettersetter") {
    val mapper = new ObjectMapper()

    mapper.registerModule(DefaultScalaModule)
    mapper.enable(SerializationFeature.INDENT_OUTPUT)
    mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE)

    val d0 = new Data3
    val d0Json = mapper.writeValueAsString(d0)

    // throws JsonMappingException
    val d1 = mapper.readValue(d0Json, classOf[Data3])
  }
}
@abhisheky2095
Copy link

How can we use @JsonAnySetter for case classes in scala?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants