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

Deserialized inner case classes cannot be compared with case classes initialized in code #564

Open
svenallers opened this issue Dec 7, 2018 · 2 comments

Comments

@svenallers
Copy link

commented Dec 7, 2018

When deserializing case classes that are defined inside of a class then those cannot be compared with objects of the same class that were initialized in code.

Example:

import org.json4s.{DefaultFormats, Formats}
import org.json4s.native.JsonMethods
import org.scalatest.{Matchers, WordSpec}

class MySpec extends WordSpec with Matchers{
  implicit val formats: Formats = DefaultFormats

  "This test fails" in {
    val json = JsonMethods.parse("""{
      |  "id": "1234"
      |}
    """.stripMargin)
    
    val model = json.extract[MyModel]
    model.id shouldBe "1234" //This check still works
    model shouldBe MyModel("1234") //This check fails
  }

  case class MyModel(id: String)
}

I fixed it for me by defining the case classes outside of the class. But I still think that this is a bug and should be fixed.

json4s version

3.6.2

scala version

2.12.8

jdk version

1.8.0_191

@mario-galic

This comment has been minimized.

Copy link

commented Apr 28, 2019

Replicated with "org.json4s" %% "json4s-native" % "3.6.5" and Scala 2.12.8: https://stackoverflow.com/questions/55890176/structural-equality-affected-by-location-of-case-class-definition-after-deserial

@magnolia-k

This comment has been minimized.

Copy link
Contributor

commented Jun 23, 2019

I think this is not a bug.

For more details, please refer to the README "Serializing classes defined in traits or classes".

Scala requires an instance of the outer class in order to instantiate nested inner classes.

Json4s automatically creates an instance of the outer class (in above example, it creates an instance of MySpec). Since the equivalence check of the case class is checked together with the instances of the outer class, they are judged to be different value.

To get around this, give an instance of the outer class as described in the README "Serializing classes defined in traits or classes".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.