Skip to content

JsonSerializable gets ignored on classes implementing Map #296

@chasebradford

Description

@chasebradford

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"

  }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions