Last active
August 29, 2015 14:05
-
-
Save igstan/9705afbf40fe5d51feb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ArgonautExamples { | |
import argonaut._, Argonaut._ | |
// 1. ------------------------------------------------------------------------ | |
// | |
// def parseThrak(json: JsValue): Seq[Thrak] = json match { | |
// case JsString(Regex1(x)) => Seq(SimpleText(x)) | |
// case JsString(Regex2(x)) => Seq(ComplexText(x)) | |
// case o @ JsObject(_) if o.containsKeys("k1") => Seq(Lov(o)) | |
// case o @ JsObject(_) if o.containsKeys("k2") => Seq(Uom(o)) | |
// case JsArray(xs) => xs.flatMap(parseThrak) | |
// case x => sys error s"oops: $x" | |
// } | |
// | |
sealed trait Thrak | |
case class SimpleText(s: String) extends Thrak | |
case class ComplexText(s: String) extends Thrak | |
case class Lov(o: Json) extends Thrak | |
case class Uom(o: Json) extends Thrak | |
object Thrak { | |
// I was too lazy to create stub extractors. | |
def matchesRegex1(x: String) = true | |
def matchesRegex2(x: String) = true | |
def textValue(cursor: HCursor): PartialFunction[String, DecodeResult[Thrak]] = { | |
case x if matchesRegex1(x) => DecodeResult.ok(SimpleText(x)) | |
case x if matchesRegex2(x) => DecodeResult.ok(ComplexText(x)) | |
case x => DecodeResult.fail(s"$x didn't match any regex", cursor.history) | |
} | |
implicit val decodeJson: DecodeJson[Thrak] = DecodeJson { cursor => | |
cursor.as[String].flatMap(textValue(cursor)) ||| | |
cursor.downField("k1").up.as[Json].map(Lov(_)) ||| | |
cursor.downField("k2").up.as[Json].map(Uom(_)) | |
} | |
} | |
def usage: Unit = { | |
val sampleJson = Json.array( | |
Json.obj("k1" := Json.obj("foo" := "bar")), | |
Json.obj("k2" := Json.obj("foo" := "bar")), | |
jString("simple-text"), | |
jString("complex-text") | |
) | |
println( sampleJson.as[List[Thrak]] ) | |
} | |
// 2. ------------------------------------------------------------------------ | |
// | |
// def processJson(json: JsValue): JsValue = json match { | |
// case JsString(str) => JsString(processString(str)) | |
// case JsArray(values) => JsArray(values.map(processJson)) | |
// case JsObject(pairs) => JsObject(pairs.map { case (k, v) => (k -> processJson(v))}) | |
// case json => json | |
// } | |
// | |
def processString(s: String) = s | |
def processJson(js: Json): Json = | |
js.withString(processString(_)) | |
.withArray(_.map(processJson)) | |
.withObject(_.map(processJson)) | |
// 3. ------------------------------------------------------------------------ | |
// Not sure about this one... | |
// 4. ------------------------------------------------------------------------ | |
// | |
// jsValues collect { | |
// case JsString(x) => x | |
// } | |
// | |
val jsValues = List(jString("foo"), jNumber(42)) | |
jsValues.collect { | |
case x if x.isString => x.stringOrEmpty | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment