Spray-json中的NPE由于递归隐式(上下文绑定问题?) [英] NPE in spray-json because of recursive implicits (context bound issue?)
问题描述
也许我在spray-json中发现了 bug .当我尝试获取具有自身类型字段的对象的json时,出现Null Pointer Exception.示例是:
Perhaps I discovered a bug in spray-json. I get Null Pointer Exception when I'm trying to get json of an object that has field of type of itself. Example is:
case class TestItem(subitems: Option[List[TestItem]])
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val testItemFormat: RootJsonFormat[TestItem] = jsonFormat(TestItem, "subitems")
}
import MyJsonProtocol._
object TestNPE {
def main(args: Array[String]) {
val subitems = List(TestItem(None))
val item: TestItem = TestItem(Option(subitems))
val jsonAst = item.toJson
val json = jsonAst.prettyPrint
println(json)
}
}
这是调用栈
Exception in thread "main" java.lang.NullPointerException
at spray.json.PimpedAny.toJson(package.scala:40)
at spray.json.CollectionFormats$$anon$1$$anonfun$write$1.apply(CollectionFormats.scala:26)
at spray.json.CollectionFormats$$anon$1$$anonfun$write$1.apply(CollectionFormats.scala:26)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
at scala.collection.immutable.List.foreach(List.scala:309)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
at scala.collection.AbstractTraversable.map(Traversable.scala:105)
at spray.json.CollectionFormats$$anon$1.write(CollectionFormats.scala:26)
at spray.json.CollectionFormats$$anon$1.write(CollectionFormats.scala:25)
at spray.json.PimpedAny.toJson(package.scala:40)
at spray.json.StandardFormats$OptionFormat.write(StandardFormats.scala:34)
at spray.json.StandardFormats$OptionFormat.write(StandardFormats.scala:32)
at spray.json.ProductFormats$class.productElement2Field(ProductFormats.scala:473)
at spray.json.MyJsonProtocol$.productElement2Field(TestNPE.scala:5)
at spray.json.ProductFormats$$anon$1.write(ProductFormats.scala:32)
at spray.json.ProductFormats$$anon$1.write(ProductFormats.scala:30)
at spray.json.PimpedAny.toJson(package.scala:40)
at spray.json.TestNPE$.main(TestNPE.scala:18)
at spray.json.TestNPE.main(TestNPE.scala)
Sooo我试图自己修复它,但是我对Scala的了解还不够强. NPE发生在此处尝试转换内部TestItem
时.此时的功能参数write
是null
.
Sooo I tried to fix it myself but my knowledge of Scala is not strong enough yet. NPE happens here when it's attempting to convert inner TestItem
. Function parameter write
at that moment is null
.
您能告诉我为什么它不使用我的隐式吗?我在调试器中看到,与其使用我的隐式write
而不是包含某些魔术字段evidence$x$y
的值,该字段随其变化深入了解通话链.我不知道那是什么我觉得这与上下文相关,但请阅读相关章节没有帮助.
Could you please explain to me why it doesn't use my implicit instead ? I see in the debugger that instead of using my implicit write
contains value of some magic field evidence$x$y
which changes as it goes deeper in calling chain. I don't know what it is. I feel like it's something related to context bound but reading relevant chapter didn't help.
推荐答案
嗯,我应该更加专心.我不得不使用lazyFormat
包装器.
Well, I should have been more attentive. I had to use lazyFormat
wrapper.
这篇关于Spray-json中的NPE由于递归隐式(上下文绑定问题?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!