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

JsonSerializable gets ignored on classes implementing Map #296

Closed
chasebradford opened this issue Nov 4, 2016 · 5 comments
Closed

JsonSerializable gets ignored on classes implementing Map #296

chasebradford opened this issue Nov 4, 2016 · 5 comments
Assignees
Labels

Comments

@chasebradford
Copy link

I have a class that extends Map[,] but implements its own Json serialization. I just jumped from 2.4.1 to 2.8.3 and I hit an error where its custom serialization doesn't get used. Instead, it attempts to serialize the object based on its Map interface. I expected the JsonSerializable implementation to take priority over any other modules matching the class.

I narrowed down the break to somewhere between tags 2.6.0-rc2 and 2.6.0-rc4. I'll keep looking, but it would be nice if someone might have a direction to point me in. Here's a test class I setup to reproduce the error. It's the one that works in rc2 but fails rc4.

import com.fasterxml.jackson.module.scala.BaseFixture

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

import com.fasterxml.jackson.databind.JsonSerializable
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.jsontype.TypeSerializer
import com.fasterxml.jackson.core.JsonGenerator

class SerializableClass extends Map[String, String] with JsonSerializable {
  def +[B1 >: String](kv: (String, B1)) = this
  def -(key: String): Map[String,String] = this

  // Members declared in scala.collection.MapLike
  def get(key: String): Option[String] = None
  def iterator: Iterator[(String, String)] = throw new IllegalArgumentException("This shouldn't get called")

  override def serialize(jgen:JsonGenerator, provider:SerializerProvider) {
    jgen.writeNumber(10)
  }
  override def serializeWithType(jgen:JsonGenerator, provider:SerializerProvider, typeSer:TypeSerializer) {
    serialize(jgen, provider)
  }

}

class SerializableTest extends BaseFixture {

  it should "use serialize method in JsonSerializable" in { mapper =>

    mapper.writeValueAsString(new SerializableClass()) shouldBe "10"

  }
}
@chasebradford
Copy link
Author

If it helps, this happens with Iterable too. It doesn't have to be a Map.

@cowtowncoder
Copy link
Member

If this can be reproduced with plain Java, would be good to file against jackson-databind.

@pjfanning pjfanning self-assigned this Sep 3, 2021
@pjfanning pjfanning added the 2.13 label Sep 3, 2021
@pjfanning
Copy link
Member

fc818b1 should fix this

@pjfanning
Copy link
Member

fix will be in 2.13 release

@cowtowncoder
Copy link
Member

Ok good. Although in some ways it'd be nice if jackson-databind could handle it in a way to let custom modules know the special status so that modules did not have to do extra introspection. But conversely it should always be possible to override this behavior (i.e. databind should not automatically assume JsonSerializable implementing things must be handled with default serializer). So I guess this makes most sense for now.

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

No branches or pull requests

3 participants