diff options
6 files changed, 39 insertions, 31 deletions
diff --git a/src/main/scala/cc/spray/json/lenses/ExtraImplicits.scala b/src/main/scala/cc/spray/json/lenses/ExtraImplicits.scala new file mode 100644 index 0000000..9951c94 --- /dev/null +++ b/src/main/scala/cc/spray/json/lenses/ExtraImplicits.scala @@ -0,0 +1,32 @@ +package cc.spray.json +package lenses + +trait ExtraImplicits { + trait RichJsValue { + def value: JsValue + + def update(updater: Update): JsValue = updater(value) + + def update[T: JsonWriter, M[_]](lens: UpdateLens, pValue: T): JsValue = + lens ! Operations.set(pValue) apply value + + // This can't be simplified because we don't want the type constructor + // of projection to appear in the type paramater list. + def extract[T: Reader](p: Projection[Id]): T = + p.get[T](value) + + def extract[T: Reader](p: Projection[Option]): Option[T] = + p.get[T](value) + + def extract[T: Reader](p: Projection[Seq]): Seq[T] = + p.get[T](value) + + def as[T: Reader]: Validated[T] = + implicitly[Reader[T]].read(value) + } + + implicit def richValue(v: JsValue): RichJsValue = new RichJsValue { def value = v } + implicit def richString(str: String): RichJsValue = new RichJsValue { def value = JsonParser(str) } +} + +object ExtraImplicits extends ExtraImplicits
\ No newline at end of file diff --git a/src/main/scala/cc/spray/json/lenses/JsonLenses.scala b/src/main/scala/cc/spray/json/lenses/JsonLenses.scala index 8ffc12c..8ebc011 100644 --- a/src/main/scala/cc/spray/json/lenses/JsonLenses.scala +++ b/src/main/scala/cc/spray/json/lenses/JsonLenses.scala @@ -10,9 +10,9 @@ object JsonLenses extends OptionLenses with SeqLenses with Operations with - JsonPathIntegration { + JsonPathIntegration with + ExtraImplicits { implicit def strToField(name: String): ScalarProjection = field(name) implicit def symbolToField(sym: Symbol): ScalarProjection = field(sym.name) - }
\ No newline at end of file diff --git a/src/main/scala/cc/spray/json/lenses/Operations.scala b/src/main/scala/cc/spray/json/lenses/Operations.scala index e539df8..20e6572 100644 --- a/src/main/scala/cc/spray/json/lenses/Operations.scala +++ b/src/main/scala/cc/spray/json/lenses/Operations.scala @@ -4,7 +4,7 @@ package lenses /** * Defines a set of operations to update Json values. */ -trait Operations { +trait Operations { _: ExtraImplicits => /** * The set operation sets or creates a value. */ @@ -36,4 +36,4 @@ trait Operations { def extract[M[_], T](value: Projection[M])(f: M[T] => Update): Operation = ??? } -object Operations extends Operations
\ No newline at end of file +object Operations extends Operations with ExtraImplicits
\ No newline at end of file diff --git a/src/main/scala/cc/spray/json/lenses/Projection.scala b/src/main/scala/cc/spray/json/lenses/Projection.scala index 35fd99d..0db5b88 100644 --- a/src/main/scala/cc/spray/json/lenses/Projection.scala +++ b/src/main/scala/cc/spray/json/lenses/Projection.scala @@ -23,8 +23,9 @@ trait Projection[M[_]] extends UpdateLens with ReadLens[M] { * This implements most of the methods of `Projection`. Implementors of a new type of projection * must implement `retr` for the read side of the lens and `updated` for the update side of the lens. */ -trait ProjectionImpl[M[_]] extends Projection[M] { - outer => +trait ProjectionImpl[M[_]] extends Projection[M] { outer => + import ExtraImplicits.richValue + def tryGet[T: Reader](p: JsValue): Validated[M[T]] = retr(p).flatMap(mapValue(_)(_.as[T])) diff --git a/src/main/scala/cc/spray/json/lenses/package.scala b/src/main/scala/cc/spray/json/lenses/package.scala index ef61b15..685a52b 100644 --- a/src/main/scala/cc/spray/json/lenses/package.scala +++ b/src/main/scala/cc/spray/json/lenses/package.scala @@ -53,27 +53,4 @@ package object lenses { } implicit def validateOption[T](o: Option[T]): ValidateOption[T] = ValidateOption(o) - - case class RichJsValue(value: JsValue) { - def update(updater: Update): JsValue = updater(value) - - def update[T: JsonWriter, M[_]](lens: UpdateLens, pValue: T): JsValue = - lens ! Operations.set(pValue) apply value - - // This can't be simplified because we don't want the type constructor - // of projection to appear in the type paramater list. - def extract[T: Reader](p: Projection[Id]): T = - p.get[T](value) - - def extract[T: Reader](p: Projection[Option]): Option[T] = - p.get[T](value) - - def extract[T: Reader](p: Projection[Seq]): Seq[T] = - p.get[T](value) - - def as[T: Reader]: Validated[T] = - implicitly[Reader[T]].read(value) - } - - implicit def updatable(value: JsValue): RichJsValue = RichJsValue(value) }
\ No newline at end of file diff --git a/src/test/scala/cc/spray/json/lenses/SpecHelpers.scala b/src/test/scala/cc/spray/json/lenses/SpecHelpers.scala index 8962a0d..c6efe87 100644 --- a/src/test/scala/cc/spray/json/lenses/SpecHelpers.scala +++ b/src/test/scala/cc/spray/json/lenses/SpecHelpers.scala @@ -20,6 +20,4 @@ trait SpecHelpers { case e => createExpectable(e.getMessage).applyMatcher(new BeMatching(".*" + Pattern.quote(message) + ".*")) } } - - implicit def richTestString(string: String): RichJsValue = RichJsValue(JsonParser(string)) } |