Разбор неструктурированного JSON с аргонавтом (из pandoc) - PullRequest
0 голосов
/ 30 марта 2019

Итак, я пытаюсь проанализировать JSON с argonaut .

Идея состоит в том, чтобы разобрать json из pandoc (пример ниже):

// 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
}

Однако эти примеры из "реального мира" намного сложнее, чем примеры из документации. Излишне говорить, что у меня возникают ошибки компиляции: Я много раз искал проблемы, подобные этим, но не мог найти ни одной. Я собираюсь перейти от аргонавта, это кажется слишком сложным!

[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"
// ...
...