Skip to content

Instantly share code, notes, and snippets.

@qingwei91
Created March 14, 2019 20:42
Show Gist options
  • Select an option

  • Save qingwei91/1f3453eb6e022255290326fb96260dbd to your computer and use it in GitHub Desktop.

Select an option

Save qingwei91/1f3453eb6e022255290326fb96260dbd to your computer and use it in GitHub Desktop.
import $ivy.`io.circe::circe-parser:0.10.0`
import io.circe.Decoder
val toDecoder: HAlgebra[QueryF, Decoder] = new HAlgebra[QueryF, Decoder] {
override def apply[A](fa: QueryF[Decoder, A]): Decoder[A] = fa match {
case QueryBoolF => Decoder.decodeBoolean
case QueryStringF => Decoder.decodeString
case q: QueryPathF[Decoder, A] =>
Decoder.instance { cursor =>
cursor.get(q.path)(q.next)
}
}
}
val decoder = hCata(toDecoder, nestedQuery)
val json = parse("""
{
"oh": {
"my": "20202"
}
}
""").right.get
decoder.decode(json) // Right("20202")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment