Skip to content
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

FIELD property accessor is overridden by GETTER property accessor #282

Open
ncanning opened this issue Sep 2, 2016 · 0 comments
Open

FIELD property accessor is overridden by GETTER property accessor #282

ncanning opened this issue Sep 2, 2016 · 0 comments

Comments

@ncanning
Copy link

ncanning commented Sep 2, 2016

Not sure exactly what's going on here. A public field gets serialized fine, until I set the visibility using PropertyAccessor.GETTER.

~/test ❯❯❯ cat src/main/scala/Test.scala
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility

import java.io.StringWriter

object Test extends App {
  val serializer = new ObjectMapper
  serializer.registerModule(DefaultScalaModule)
  //serializer.setVisibility(PropertyAccessor.GETTER, Visibility.NONE)
  serializer.setVisibility(PropertyAccessor.FIELD, Visibility.PROTECTED_AND_PUBLIC)

  val test = new Test
  test.addString("a")
  println(test.toJson)
}

class Test {

  var entries = Seq[String]()

  def addString(str: String) = {
    entries = entries :+ str
  }

  def toJson(): String = {
    val writer = new StringWriter
    Test.serializer.writeValue(writer, this)
    return writer.toString
  }
}

produces {"entries":["a"]}, but uncommenting the GETTER line produces {}. Not sure if I'm missing something about the way PropretyAccessor.GETTER works, but this seems like a bug.

Here's my build.sbt as well, in case it's a version-specific problem:

~/test ❯❯❯ cat build.sbt
name := "Test"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies ++= Seq(
  "com.fasterxml.jackson.module" % "jackson-module-scala_2.11" % "2.7.5"
)
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

1 participant