Skip to content
Browse files

parse then decode

  • Loading branch information...
1 parent 0a75ab5 commit 6c051be52229d36004784834dde39cded0dddb36 @tonymorris tonymorris committed Aug 31, 2012
Showing with 10 additions and 1 deletion.
  1. +10 −1 src/main/scala/com/ephox/argonaut/StringWrap.scala
View
11 src/main/scala/com/ephox/argonaut/StringWrap.scala
@@ -25,7 +25,7 @@ sealed trait StringWrap {
* @param err Run this function if the parse produces an error.
* @param failure Run this function if the parse produces a failure.
*/
- def parse[X](success: Json => X, err: String => X, failure: String => X) = {
+ def parse[X](success: Json => X, err: String => X, failure: String => X): X = {
val p = new JsonParser
val r = new CharSequenceReader(value)
p.jvalue(r) match {
@@ -36,6 +36,15 @@ sealed trait StringWrap {
}
/**
+ * Parses this string value into a JSON value and if it succeeds, decodes to a data-type.
+ *
+ * @param err Run this function if the parse produces an error.
+ * @param failure Run this function if the parse produces a failure.
+ */
+ def decodeparse[X: DecodeJson](err: String => X, failure: String => X): DecodeResult[X] =
+ parse(_.jdecode, s => DecodeResult(err(s)), s => DecodeResult(failure(s)))
+
+ /**
* Parses this string value and executes one of the given functions, depending on the parse outcome. The distinction
* between a parse `error` and a `failure` is not made by this function.
*

0 comments on commit 6c051be

Please sign in to comment.
Something went wrong with that request. Please try again.