MappingException:使用 json4s 时出现未知错误
MappingException: unknown error using json4s
我正在尝试使用 json4s 解析一个简单的 Json,我发现它可以在我的程序的 Main class 中运行,但由于某种原因不能在单元测试中运行。这是一个最小的例子:
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",
)
包含案例 class 定义和函数的文件:
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")
}
}
我知道您的案例 class 不在包的顶层有一些陷阱,但据我所知,我还没有踩到那个特定的我的。谁能伸出援手?如果这被证明有一个简单的解决方案,冒着听起来像个白痴的风险,这已经花费了我几个小时的工作。
我的堆栈跟踪如下所示:
[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(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(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
...
这很好地表明 order of initialization problem。
您在 ProcessInput
中使用 import Main.formats
,在 Main
中使用 ProcessInput
。我无法弄清楚为什么它在 Main 中起作用但在单元测试中不起作用,但我建议将 formats
的定义移动到 ProcessInput
或使其成为 captureEasyInput
的隐式参数。
我正在尝试使用 json4s 解析一个简单的 Json,我发现它可以在我的程序的 Main class 中运行,但由于某种原因不能在单元测试中运行。这是一个最小的例子:
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",
)
包含案例 class 定义和函数的文件:
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")
}
}
我知道您的案例 class 不在包的顶层有一些陷阱,但据我所知,我还没有踩到那个特定的我的。谁能伸出援手?如果这被证明有一个简单的解决方案,冒着听起来像个白痴的风险,这已经花费了我几个小时的工作。
我的堆栈跟踪如下所示:
[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(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(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
...
这很好地表明 order of initialization problem。
您在 ProcessInput
中使用 import Main.formats
,在 Main
中使用 ProcessInput
。我无法弄清楚为什么它在 Main 中起作用但在单元测试中不起作用,但我建议将 formats
的定义移动到 ProcessInput
或使其成为 captureEasyInput
的隐式参数。