Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 846
Mario Galic Avatar asked Mar 05 '23 02:03

Mario Galic


1 Answers

This is a bug.

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

like image 67
Dmytro Mitin Avatar answered Apr 12 '23 22:04

Dmytro Mitin