问题描述
为什么将结构相等性比较反序列化为案例类实例之后,会受到案例类定义在另一个类之内还是之外的位置的影响.例如,以下代码段
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
}
失败
`java.lang.AssertionError: assertion failed: Person(picard) == Person(picard)`
如果我们将 Person
的定义移到 Foo
类之外的话,它就会通过
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)
不管 case class Person
定义位置如何,
都满足.
is satisfied irrespective of case class Person
definition location.
也许它受到传入Manifest 的某种影响/org/json4s/ExtractableJsonAstNode.scala#L20"rel =" nofollow noreferrer> 提取
?
Perhaps it is somehow affected by the implicit Manifest
passed in to extract
?
推荐答案
这是一个错误.
https://github.com/json4s/json4s/issues/564 反序列化的内部案例类无法与代码中初始化的案例类进行比较"
https://github.com/json4s/json4s/issues/564"Deserialized inner case classes cannot be compared with case classes initialized in code"
这篇关于反序列化后,结构平等受案例类定义位置的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!