summaryrefslogtreecommitdiff
path: root/src/main/scala/cc/spray/json/lenses/ReadLens.scala
blob: 80b3d3784d7edb89d888766fe47750083e8d1471 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cc.spray.json
package lenses

/**
 * The read lens can extract child values out of a JsValue hierarchy. A read lens
 * is parameterized with a type constructor. This allows to extracts not only scalar
 * values but also sequences or optional values.
 * @tparam M
 */
trait ReadLens[M[_]] {
  /**
   * Given a parent JsValue, tries to extract the child value.
   * @return `Right(value)` if the projection succeeds. `Left(error)` if the projection
   *         fails.
   */
  def retr: JsValue => Validated[M[JsValue]]

  /**
   * Given a parent JsValue extracts and tries to convert the JsValue into
   * a value of type `T`
   */
  def tryGet[T: Reader](value: JsValue): Validated[M[T]]

  /**
   * Given a parent JsValue extracts and converts a JsValue into a value of
   * type `T` or throws an exception.
   */
  def get[T: Reader](value: JsValue): M[T]

  /**
   * Lifts a predicate for a converted value for this lens up to the
   * parent level.
   */
  def is[U: Reader](f: U => Boolean): JsPred
}