Scala 解读成功的历史?

Scala 解读成功的历史?,scala,argonaut,Scala,Argonaut,鉴于: import argonaut.\uu,argonaut_ 案例类人员(名称:字符串) 隐式def decode:DecodeJson[Person]= 解码JSON(c=> 为了{ name Parse.decode[Person](“”“{”name:“Bob”,“foo:“dunno”}“”) res5:other[other[String,(String,argonaut.CursorHistory)],Person]= 对(人(鲍勃)) 我怎样才能用光标的历史来解码,即JSO

鉴于:

import argonaut.\uu,argonaut_
案例类人员(名称:字符串)
隐式def decode:DecodeJson[Person]=
解码JSON(c=>
为了{
name Parse.decode[Person](“”“{”name:“Bob”,“foo:“dunno”}“”)
res5:other[other[String,(String,argonaut.CursorHistory)],Person]=
对(人(鲍勃))

我怎样才能用光标的历史来解码,即
JSON=>Person
?我的意思是,我想知道
“foo”:“dunno”
没有被查看/遍历。

不幸的是,当为成功案例构建
解码结果[T]
对象时,光标历史被丢弃

我能想到的唯一解决方案(这只是一个存根来向您解释这个概念)是:

  • 实现自定义
    历史解码结果
类历史解码结果[A](
val h:选项[CursorHistory],
结果:\/[(字符串,游标历史记录),A]
)扩展解码结果[A](结果)
对象历史解码结果{
def apply[A](h:Option[CursorHistory],d:DecodeResult[A])=新历史解码结果(h,d.result)
}
  • 隐式扩展
    ACursor
    添加助手
    asH
    方法
隐式类AHCursor(a:ACursor){
def asH[A](隐式d:DecodeJson[A]):HistoryDecodeResult[A]={
HistoryDecodeResult(a.hcursor.map(u.history),a.as[a](d))
}
}
  • HistoryDecodeResult
    中覆盖
    DecodeResult[T]
    ,以累积历史记录:
覆盖def映射[B](f:A=>B):历史解码结果[B]=
HistoryDecodeResult(h,super.map(f))
//使用`CursorHistory.++`如果使用另一个HistoryDecodeResult进行平面映射,则累积历史`h`
覆盖def flatMap[B](f:A=>DecodeResult[B]):HistoryDecodeResult[B]=???
…//继续使用其他方法
  • 从解码例程中获取
    HistoryDecodeResult
    (必须避免
    Parse.decode
    )并询问历史记录
    • 根据,您可以使用hcursor代替解码器,这样您就可以跟踪解码过程

      import argonaut._, Argonaut._
      
      case class Person(name: String)
      
      implicit def decode: DecodeJson[Person] =
        DecodeJson ( c => 
          for {
            name <- (c --\ "name").as[String]
          } yield Person(name)
        )
      
      scala> Parse.decode[Person]("""{"name": "Bob", "foo": "dunno"}""")
      res5: Either[Either[String,(String, argonaut.CursorHistory)],Person] = 
        Right(Person(Bob))