所以我正在尝试使用argonaut解析JSON。
想法是从pandoc(下面的示例)中解析json:
// example.scala
object Pandoc {
type Message = List[Content]
trait Content
// implicit def ContentCodecJson: CodecJson[Content] =
// casecodec2(Unmeta.apply, Unmeta.unapply)("unMeta")
case class Unmeta(unmeta: Json) extends Content
// implicit def UnmetaCodecJson: CodecJson[Unmeta] =
// casecodec2(Unmeta.apply, Unmeta.unapply)("unMeta")
case class AllElements(a: List[JsonElement]) extends Content
// implicit def AllElementsCodecJson: CodecJson[AllElements] =
// casecodec1(JsonElement.apply, JsonElement.unapply)("a")
// implicit def JsonElementCodecJson: CodecJson[JsonElement] =
// casecodec2(JsonElement.apply, JsonElement.unapply)("t", "c")
case class JsonElement(t: String, c: Any)
implicit def ContentDecodeJson: DecodeJson[Content] =
DecodeJson(
c =>
c match {
case _: Unmeta ⇒ {
???
}
case _: AllElements ⇒ {
???
}
case _ ⇒ throw new Exception()
}
)
}
object Example {
// raw file: `Paragraph 01.` ; converted with: `pandoc --to json ./other/example/example01.md`
val string = """
| [
| {
| "unMeta": {}
| },
| [
| {
| "t": "Para",
| "c": [
| {
| "t": "Str",
| "c": "Paragraph"
| },
| {
| "t": "Space",
| "c": []
| },
| {
| "t": "Str",
| "c": "01."
| }
| ]
| }
| ]
| ]
""".stripMargin
}
但是,这些“真实世界”示例比文档中的示例复杂得多。不用说我遇到了编译错误: 我已经广泛搜索了类似的问题,但找不到任何问题。我要从argonaut切换,这似乎太复杂了!
[info] Compiling 1 Scala source to /path/scala_pandoc/target/scala-2.12/classes ...
[error] /path/scala_pandoc/src/main/scala/example/Example.scala:43:19: fruitless type test: a value of type argonaut.HCursor cannot also be a scalapandoc.Pandoc.Unmeta
[error] case _: Unmeta ⇒ {
[error] ^
[error] /path/scala_pandoc/src/main/scala/example/Example.scala:46:19: fruitless type test: a value of type argonaut.HCursor cannot also be a scalapandoc.Pandoc.AllElements
[error] case _: AllElements ⇒ {
[error] ^
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 0 s, completed Mar 29, 2019 9:55:16 PM
77. Waiting for source changes in project root... (press enter to interrupt)
[info] Compiling 1 Scala source to /path/scala_pandoc/target/scala-2.12/classes ...
[error] /path/scala_pandoc/src/main/scala/example/Example.scala:43:19: fruitless type test: a value of type argonaut.HCursor cannot also be a scalapandoc.Pandoc.Unmeta
[error] case _: Unmeta ⇒ {
[error] ^
[error] /path/scala_pandoc/src/main/scala/example/Example.scala:46:19: fruitless type test: a value of type argonaut.HCursor cannot also be a scalapandoc.Pandoc.AllElements
[error] case _: AllElements ⇒ {
[error] ^
[error] two errors found
[error] (Compile / compileIncremental) Compilation failed
build.sbt
的摘录:
// ...
scalaVersion := "2.12.8",
// ...
lazy val argonaut = "io.argonaut" %% "argonaut" % "6.2.2"
// ...