Skip to content

Commit

Permalink
Do not use Either in parser API. It propably simpler just to throw an…
Browse files Browse the repository at this point in the history
… exception.
  • Loading branch information
Joni Freeman committed Aug 12, 2009
1 parent 5405b91 commit 22147cf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
6 changes: 3 additions & 3 deletions lift-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Any valid json can be parsed into internal AST format.

scala> import net.liftweb.json.JsonParser._
scala> parse(""" { "numbers" : [1, 2, 3, 4] } """)
res0: Either[net.liftweb.json.JsonParser.ParseError,net.liftweb.json.JsonAST.JValue] =
Right(JObject(List(JField(numbers,JArray(List(JInt(1), JInt(2), JInt(3), JInt(4)))))))
res0: net.liftweb.json.JsonAST.JValue =
JObject(List(JField(numbers,JArray(List(JInt(1), JInt(2), JInt(3), JInt(4))))))

Queries
-------
Expand Down Expand Up @@ -183,7 +183,7 @@ Indexed path expressions work too.
}
]
}
""").right.get
""")

scala> (json \ "children")(0)
res0: net.liftweb.json.JsonAST.JValue = JObject(List(JField(name,JString(Mary)), JField(age,JInt(5))))
Expand Down
8 changes: 4 additions & 4 deletions lift-json/src/main/scala/net/liftweb/json/JsonParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package net.liftweb.json
object JsonParser {
import JsonAST._

case class ParseError(message: String)
class ParseException(message: String) extends Exception

sealed abstract class Token
case object OpenObj extends Token
Expand Down Expand Up @@ -61,11 +61,11 @@ object JsonParser {
def toJValue = JArray(elems.map(_.toJValue).reverse)
}

def parse(s: String): Either[ParseError, JValue] =
def parse(s: String): JValue =
try {
Right(parse0(s))
parse0(s)
} catch {
case e: Exception => Left(ParseError(e.getMessage))
case e: Exception => throw new ParseException(e.getMessage)
}

private def parse0(s: String): JValue = {
Expand Down
55 changes: 19 additions & 36 deletions lift-json/src/test/scala/net/liftweb/json/Examples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,40 @@ object Examples extends Specification {
import JsonParser._

"Lotto example" in {
parse(lotto) match {
case Right(json) =>
val renderedLotto = compact(render(json))
json mustEqual parse(renderedLotto).right.get
case Left(err) => fail(err.message)
}
val json = parse(lotto)
val renderedLotto = compact(render(json))
json mustEqual parse(renderedLotto)
}

"Person example" in {
parse(person) match {
case Right(json) =>
val renderedPerson = JsonDSL.pretty(render(json))
json mustEqual parse(renderedPerson).right.get
render(json) mustEqual render(personDSL)
compact(render(json \\ "name")) mustEqual """{"name":"Joe","name":"Marilyn"}"""
compact(render(json \ "person" \ "name")) mustEqual "\"name\":\"Joe\""
case Left(err) => fail(err.message)
}
val json = parse(person)
val renderedPerson = JsonDSL.pretty(render(json))
json mustEqual parse(renderedPerson)
render(json) mustEqual render(personDSL)
compact(render(json \\ "name")) mustEqual """{"name":"Joe","name":"Marilyn"}"""
compact(render(json \ "person" \ "name")) mustEqual "\"name\":\"Joe\""
}

"Object array example" in {
parse(objArray) match {
case Right(json) =>
compact(render(json \ "children" \ "name")) mustEqual """["name":"Mary","name":"Mazy"]"""
compact(render((json \ "children")(0) \ "name")) mustEqual "\"name\":\"Mary\""
compact(render((json \ "children")(1) \ "name")) mustEqual "\"name\":\"Mazy\""
case Left(err) => fail(err.message)
}
val json = parse(objArray)
compact(render(json \ "children" \ "name")) mustEqual """["name":"Mary","name":"Mazy"]"""
compact(render((json \ "children")(0) \ "name")) mustEqual "\"name\":\"Mary\""
compact(render((json \ "children")(1) \ "name")) mustEqual "\"name\":\"Mazy\""
}

"Quoted example" in {
parse(quoted) match {
case Right(json) => List("foo \" \n \t \r bar") mustEqual json.values
case Left(err) => fail(err.message)
}
val json = parse(quoted)
List("foo \" \n \t \r bar") mustEqual json.values
}

"Extraction example" in {
parse(objArray) match {
case Right(json) =>
json.extract[Person] mustEqual Person("joe", Address("Bulevard", "Helsinki"), List(Child("Mary", BigInt(5)), Child("Mazy", BigInt(3))))
case Left(err) => fail(err.message)
}
val json = parse(objArray)
json.extract[Person] mustEqual Person("joe", Address("Bulevard", "Helsinki"), List(Child("Mary", BigInt(5)), Child("Mazy", BigInt(3))))
}

"Partial extraction example" in {
parse(objArray) match {
case Right(json) =>
json.extract[SimplePerson] mustEqual SimplePerson("joe", Address("Bulevard", "Helsinki"))
case Left(err) => fail(err.message)
}
val json = parse(objArray)
json.extract[SimplePerson] mustEqual SimplePerson("joe", Address("Bulevard", "Helsinki"))
}

val lotto = """
Expand Down
2 changes: 1 addition & 1 deletion lift-json/src/test/scala/net/liftweb/json/ParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object ParserSpec extends Specification with JValueGen with ScalaCheck {
import JsonParser._

"Any valid json can be parsed" in {
val parsing = (json: JValue) => parse(JsonDSL.pretty(render(json))).isRight
val parsing = (json: JValue) => { parse(JsonDSL.pretty(render(json))); true }
forAll(parsing) must pass
}

Expand Down

0 comments on commit 22147cf

Please sign in to comment.