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

Cannot serialize option of anyval in a collection #277

Open
devshorts opened this issue Aug 22, 2016 · 4 comments
Open

Cannot serialize option of anyval in a collection #277

devshorts opened this issue Aug 22, 2016 · 4 comments

Comments

@devshorts
Copy link

devshorts commented Aug 22, 2016

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?

@devshorts
Copy link
Author

As a temporary workaround I disabled anyval support in my application and to get the json to be over the wire the same I added the @(JsonValue @getter) annotation to the wrapped field.

@Assarin
Copy link

Assarin commented May 30, 2019

Having the same issue, is there is work around?

@akropp-stripe
Copy link

akropp-stripe commented May 30, 2019

@Assarin I in general moved off of anyval support for all my scala stuff. It was too problematic. I instead created this https://github.com/paradoxical-io/scala-global/tree/master/jackson/src/main/scala/io/paradoxical/jackson which is custom jackson module that knows how to serialize and deserialize tagged types using a trait:

https://github.com/paradoxical-io/scala-global/blob/master/global/src/main/scala/io/paradoxical/global/tiny/Tiny.scala

which i use like this:

https://github.com/paradoxical-io/scala-global/blob/master/jackson/src/test/scala/io/paradoxical/jackson/TinyTests.scala

I found this to be easier to give me the NewType support but still play nice with serialization/swagger/reflection/etc.

These jars are available on maven central if you are interested

@plokhotnyuk
Copy link

@Assarin yet another option is to use jsoniter-scala - it has support of AnyVals with any combination of Scala collection and other types.
Also it is much safe and efficient than other options.

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

4 participants