Scala MappingException:使用json4s时出现未知错误

Scala MappingException:使用json4s时出现未知错误,scala,json4s,Scala,Json4s,我正在尝试使用json4s解析一个简单的Json,我发现它可以在我的程序的主类中工作,但由于某些原因不能在单元测试中工作。下面是一个简单的例子: build.sbt: ThisBuild / scalaVersion := "2.13.2" lazy val validatorbroken = (project in file(".")) .settings( name := "validatorbroken", ) libraryDependencies ++= S

我正在尝试使用json4s解析一个简单的Json,我发现它可以在我的程序的主类中工作,但由于某些原因不能在单元测试中工作。下面是一个简单的例子:

build.sbt:

ThisBuild / scalaVersion     := "2.13.2"

lazy val validatorbroken = (project in file("."))
  .settings(
    name := "validatorbroken",
  )

libraryDependencies ++= Seq(
  "org.json4s" %% "json4s-jackson" % "3.7.0-M4",
  "org.scalatest" %% "scalatest" % "3.1.2" % "test",
)
包含案例类定义和函数的文件:

package validatorbroken

import org.json4s._
import org.json4s.jackson.JsonMethods._

case class EasyInput(file: String, sheet: String)

object ProcessInput {
  import Main.formats
  def captureEasyInput(s: String): EasyInput = {
    println(s"STRING JSON $s")
    val rawJson = parse(s)
    println(s"PARSED JSON $rawJson")
    rawJson.extract[EasyInput]
  }
}
工作正常的主文件:

package validatorbroken

import org.json4s._
import scala.io.Source

object Main extends App {
  implicit val formats = DefaultFormats
  val easyInputJson: String = Source.fromResource("easy_input.json").mkString
  val capturedJson = ProcessInput.captureEasyInput(easyInputJson)
  println(s"CAPTURED $capturedJson")
}
以及单元测试,它不:

package validatorbroken 

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import scala.io.Source

class UnitTests extends AnyFlatSpec with Matchers {
  implicit val formats = Main.formats
  import ProcessInput._

  "captureInput Function" should "obtain an instance of Input" in {
    val easyInputJson: String = Source.fromResource("easy_input.json").mkString
    val capturedJson = ProcessInput.captureEasyInput(easyInputJson)
    println(s"CAPTURED $capturedJson")
  }
}
我知道,让您的案例类不在包的顶层存在一些问题,但据我所知,我还没有涉足这个特定的问题。谁能帮帮忙吗?冒着听起来像白痴的风险,如果这被证明是一个简单的解决方案,这已经花费了我几个小时的工作

我的堆栈跟踪如下所示:

[info] - should obtain an instance of Input *** FAILED ***
[info]   org.json4s.package$MappingException: unknown error
[info]   at org.json4s.Extraction$.extract(Extraction.scala:46)
[info]   at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
[info]   at validatorbroken.ProcessInput$.captureEasyInput(InputToItems.scala:19)
[info]   at validatorbroken.UnitTests.$anonfun$new$1(Basic.scala:13)
[info]   at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
[info]   at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
[info]   at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
[info]   at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:22)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:20)
[info]   ...
[info]   Cause: java.lang.NullPointerException:
[info]   at org.json4s.Formats$.customDeserializer(Formats.scala:54)
[info]   at org.json4s.Extraction$.customOrElse(Extraction.scala:662)
[info]   at org.json4s.Extraction$.extract(Extraction.scala:410)
[info]   at org.json4s.Extraction$.extract(Extraction.scala:42)
[info]   at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
[info]   at validatorbroken.ProcessInput$.captureEasyInput(InputToItems.scala:19)
[info]   at validatorbroken.UnitTests.$anonfun$new$1(Basic.scala:13)
[info]   at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
[info]   at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
[info]   at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
事先谢谢你的帮助,事后也谢谢你

...
Cause: java.lang.NullPointerException
...
这是一个很好的迹象


ProcessInput
中使用
import Main.formats
,在
Main
中使用
ProcessInput
。我不能确切地解释为什么它在Main中有效,但在单元测试中无效,但我建议将
格式的定义移动到
ProcessInput
,或者将其作为
captureAeasyInput
的隐式参数。将格式定义移动到ProcessInput可以修复它。谢谢,我以前在Python中经常遇到这样的问题,但我有一个想法(显然很幼稚),那就是在编译语言中问题会少一些。在这里闭嘴,知道是我错了,而不是图书馆错了,感觉真好!