当我在SBT任务中使用json4s将对象转换为json时,它报告错误,如何修复?

时间:2015-05-14 05:27:13

标签: json scala sbt json4s

我正在写一个SBT任务,它会将一些对象转换为json并输出,但是有错误。代码是:

项目/ plugins.sbt

libraryDependencies ++= Seq(
  "org.scala-lang" % "scalap" % "2.10.4",
  "org.json4s" %% "json4s-native" % "3.2.6",
  "org.json4s" %% "json4s-core" % "3.2.6",
  "org.json4s" %% "json4s-ext" % "3.2.6"
)

build.sbt

name := "json4s-210-test"

version := "1.0"

scalaVersion := "2.10.4"

import scala.tools.scalap.scalax.rules.scalasig.ScalaSigSymbol

lazy val json = taskKey[Unit]("output a json with json4s")

json := {
  case class Config(name: String)
  case class Project(name: String, configs: Seq[Config])
  implicit val formats = org.json4s.DefaultFormats
  val project = Project("myproject", Seq(Config("conf1")))
  val json = org.json4s.native.Serialization.write(project)
  println(json)
}

运行sbt json时,会报告错误:

➜  json4s-210-test git:(master) ✗ ./sbt
[info] Loading global plugins from /Users/twer/.sbt/0.13/plugins
[info] Loading project definition from /Users/twer/workspace/json4s-210-test/project
[info] Set current project to json4s-210-test (in build file:/Users/twer/workspace/json4s-210-test/)
> json
[trace] Stack trace suppressed: run last *:json for the full output.
[error] (*:json) java.lang.NoClassDefFoundError: scala/tools/scalap/scalax/rules/scalasig/ScalaSigSymbol
[error] Total time: 0 s, completed May 14, 2015 1:19:58 PM
> last
java.lang.NoClassDefFoundError: scala/tools/scalap/scalax/rules/scalasig/ScalaSigSymbol
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$constructorsAndCompanion$3$$anonfun$13.apply(Reflector.scala:123)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$constructorsAndCompanion$3$$anonfun$13.apply(Reflector.scala:122)
    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.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47)

但是你可以看到我已经添加了依赖项:

"org.scala-lang" % "scalap" % "2.10.4"

甚至在构建文件中导入了所需的类:

import scala.tools.scalap.scalax.rules.scalasig.ScalaSigSymbol

为什么会出现这样的错误以及如何解决?

更新:您可以克隆代码并从此处开始尝试:https://github.com/freewind/json4s-210-test

0 个答案:

没有答案