Search code examples
scalaequalsjson4s

Structural equality affected by location of case class definition after deserialisation


Why is structural equality comparison affected, after deserialisation to case class instance, by the location of case class definition being inside or outside another class. For example, the assertion in the following snippet

package example

import org.json4s.DefaultFormats
import org.json4s.native.JsonMethods.parse

class Foo {
  case class Person(name: String)
  def bar = {
    implicit val formats = DefaultFormats
    val expected = Person(name = "picard")
    val actual = parse("""{"name": "picard"}""").extract[Person]
    assert(expected == actual, s"$expected == $actual")
  }
}

object Main extends App {
  (new Foo).bar
}

fails with

`java.lang.AssertionError: assertion failed: Person(picard) == Person(picard)`

whilst it passes if we move Person definition outside class Foo like so

case class Person(name: String)
class Foo {
  def bar = {
    ...
    assert(expected == actual, s"$expected == $actual")
  }
}

Note, in both cases, deserialisation seems to be successful, for example,

assert(expected.name == actual.name)

is satisfied irrespective of case class Person definition location.

Perhaps it is somehow affected by the implicit Manifest passed in to extract?


Solution

  • This is a bug.

    https://github.com/json4s/json4s/issues/564 "Deserialized inner case classes cannot be compared with case classes initialized in code"