FasterXML / FasterXML/jackson-module-scala

Cannot serialize option of anyval in a collection

Open
#277 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Scala
Stars
506
Forks
138
Avg merge
3h 6m
Merged PRs (30d)
48

Description

I have a sample test here:

```
"Anyvals" should "serialize json" in {
val groups = (0 until 10).map(_ =>
AnyHolder(
Some(TestAny("data"))
))

val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule)
println(objectMapper.writeValueAsString(data))
}
}

case class AnyHolder(
testAny: Option[TestAny]
)

case class TestAny(data: String) extends AnyVal
```

This fails with

```
tests.TestAny cannot be cast to java.lang.String (through reference chain: scala.collection.convert.IterableWrapper[0]->tests.AnyHolder["testAny"])
com.fasterxml.jackson.databind.JsonMappingException: tests.TestAny cannot be cast to java.lang.String (through reference chain: scala.collection.convert.IterableWrapper[0]->tests.AnyHolder["testAny"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:378)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:338)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:342)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:686)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)
at com.fasterxml.jackson.module.scala.ser.IterableSerializer$class.serializeContents(IterableSerializerModule.scala:28)
at com.fasterxml.jackson.module.scala.ser.UnresolvedIterableSerializer.serializeContents(IterableSerializerModule.scala:56)
```

Using jackson-core 2.7.4, jackson-databind 2.7.4, jackson-scala-module 2.7.4

Are there any workarounds to get this to work?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.