Skip to content

Commit

Permalink
Refactor such that codecs carry the cursor history.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonymorris committed Jul 11, 2012
1 parent 81bfd72 commit 6e2f43c
Show file tree
Hide file tree
Showing 20 changed files with 1,268 additions and 1,065 deletions.
112 changes: 81 additions & 31 deletions example/src/main/scala/com/ephox/argonaut/CodecDemo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,92 @@ package argonaut
import Argonaut._
import scalaz._, Scalaz._

/*
=============
JSON encoding
-------------
[
"Fred",
"40"
]
JSON decoding
-------------
Person(Fred,40)
=============
=============
JSON encoding
-------------
{
"name" : "Fred",
"age" : "40"
}
JSON decoding
-------------
Person(Fred,40)
=============
*/
object CodecDemo {
case class Person(name: String, age: Int, spouse: Option[Person], children: List[Person])
case class Person(name: String, age: Int)

object Person {
implicit val DecodePerson: DecodeJson[Person] =
DecodeJson(j => j.array match {
case Some(n::a::s::c::Nil) =>
for {
nn <- n.jdecode[String]
aa <- a.jdecode[Int]
ss <- s.jdecode[Option[Person]]
cc <- c.jdecode[List[Person]]
} yield Person(nn, aa, ss, cc)
case _ => decodeError(j, "Person")
})

implicit val EncodePerson: EncodeJson[Person] =
EncodeJson({
case Person(n, a, s, c) => jArray(List(
n.jencode
, a.jencode
, s.jencode
, c.jencode
))
}, "Person")
val fred = Person("Fred", 40)

trait Run {
implicit val DecodePerson: DecodeJson[Person]
implicit val EncodePerson: EncodeJson[Person]
def run: (String, String) = {
val enc = fred.jencode
val encP = enc.spaces2
val dec = enc.jdecode[Person]
val decP =
dec.result match {
case Left(e) => e.shows
case Right(p) => p.toString
}
(encP, decP)
}
def print {
val (enc, dec) = run
List(
"============="
, "JSON encoding"
, "-------------"
, enc
, ""
, "JSON decoding"
, "-------------"
, dec
, "============="
, ""
) foreach println
}
}

object ArrayCodec extends Run {
implicit val DecodePerson: DecodeJson[Person] =
jdecode2(Person(_: String, _: Int)) setName "Person"

implicit val EncodePerson: EncodeJson[Person] =
jencode2((p: Person) => (p.name, p.age)) setName "Person"
}

object ObjectCodec extends Run {
implicit val DecodePerson: DecodeJson[Person] =
jdecode2L(Person(_: String, _: Int))("name", "age") setName "Person"

implicit val EncodePerson: EncodeJson[Person] =
jencode2L((p: Person) => (p.name, p.age))("name", "age") setName "Person"
}

}

def main(args: Array[String]) {
val children = List(Person("Bob", 10, None, Nil), Person("Jill", 12, None, Nil))
val fred = Person("Fred", 40, Some(Person("Mary", 41, None, children)), Person("Tom", 15, None, Nil) :: children)
val enc = fred.jencode
println(enc.spaces2)
val decode = enc.jdecode[Person]
println(decode.run match {
case Left(e) => e
case Right(p) => p.toString
})
import Person._

ArrayCodec.print
ObjectCodec.print
}
}
64 changes: 0 additions & 64 deletions example/src/main/scala/com/ephox/argonaut/ShiftDemo.scala

This file was deleted.

Loading

0 comments on commit 6e2f43c

Please sign in to comment.