diff --git a/lift-json/README.md b/lift-json/README.md index f393b8aafb..42ce835198 100644 --- a/lift-json/README.md +++ b/lift-json/README.md @@ -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 ------- @@ -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)))) diff --git a/lift-json/src/main/scala/net/liftweb/json/JsonParser.scala b/lift-json/src/main/scala/net/liftweb/json/JsonParser.scala index b5e8dcc11b..5958aaef85 100644 --- a/lift-json/src/main/scala/net/liftweb/json/JsonParser.scala +++ b/lift-json/src/main/scala/net/liftweb/json/JsonParser.scala @@ -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 @@ -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 = { diff --git a/lift-json/src/test/scala/net/liftweb/json/Examples.scala b/lift-json/src/test/scala/net/liftweb/json/Examples.scala index 7dd3b85f66..19e7870e05 100644 --- a/lift-json/src/test/scala/net/liftweb/json/Examples.scala +++ b/lift-json/src/test/scala/net/liftweb/json/Examples.scala @@ -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 = """ diff --git a/lift-json/src/test/scala/net/liftweb/json/ParserTest.scala b/lift-json/src/test/scala/net/liftweb/json/ParserTest.scala index 975e2a8bab..dfd72bc24a 100644 --- a/lift-json/src/test/scala/net/liftweb/json/ParserTest.scala +++ b/lift-json/src/test/scala/net/liftweb/json/ParserTest.scala @@ -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 }