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

Unsound deserialization involving AnyVal on scala 3 #1453

Open
martijnhoekstra opened this issue Apr 11, 2024 · 0 comments
Open

Unsound deserialization involving AnyVal on scala 3 #1453

martijnhoekstra opened this issue Apr 11, 2024 · 0 comments

Comments

@martijnhoekstra
Copy link

json4s version

4.0.7

scala version

3.4.1, 3.3.3

jdk version

8, 11, 17, 21

see https://scastie.scala-lang.org/kNYNFfjiS32A8oLJ7VR9pA

case class Example(elem: String) extends AnyVal

case object ExampleSerializer
  extends CustomSerializer[Example](_ =>
    (
      { case JString(s) => Example(s) },
      { case Example(s) => JString(s) },
    ),
  )

case class Outer(examples: List[Example])

implicit val formats: Formats = DefaultFormats + ExampleSerializer

val in = read[Outer](write(o))
val firstExample: Example = in.examples.head // class java.lang.String cannot be cast to class Example

Removing the AnyVal makes this work again.

@martijnhoekstra martijnhoekstra changed the title Unsound deserialization involving AnyVal Unsound deserialization involving AnyVal on scala 3 Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant