Skip to content

Commit

Permalink
Use Jawn Parser for Argonaut
Browse files Browse the repository at this point in the history
  • Loading branch information
rpless committed Nov 17, 2015
1 parent c9c4bd7 commit d2316b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 12 additions & 7 deletions argonaut/src/main/scala/io/finch/argonaut/package.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.finch

import _root_.argonaut.{DecodeJson, EncodeJson, Parse}
import _root_.argonaut.{CursorHistory, DecodeJson, EncodeJson, Json}
import com.twitter.util.{Return, Throw, Try}
import jawn.Parser
import jawn.support.argonaut.Parser.facade
import scala.util.{Failure, Success}

package object argonaut {

Expand All @@ -10,12 +13,14 @@ package object argonaut {
* @tparam A The type of data that the ''DecodeJson'' will decode into
* @return Create a Finch ''DecodeRequest'' from an argonaut ''DecodeJson''
*/
implicit def decodeArgonaut[A](implicit decode: DecodeJson[A]): DecodeRequest[A] = DecodeRequest.instance(s =>
Parse.decodeEither(s).fold[Try[A]](
error => Throw[A](Error(error)),
Return(_)
)
)

implicit def decodeArgonaut[A](implicit decode: DecodeJson[A]): DecodeRequest[A] = DecodeRequest.instance[A] { s =>
val err: (String, CursorHistory) => Try[A] = { (str, hist) => Throw[A](Error(str)) }
Parser.parseFromString[Json](s)(facade) match {
case Success(v) => decode.decodeJson(v).fold[Try[A]](err, Return(_))
case Failure(error) => Throw[A](Error(error.getMessage))
}
}

/**
* @param encode The argonaut ''EncodeJson'' to use for decoding
Expand Down
6 changes: 5 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ lazy val petstore = project
lazy val argonaut = project
.settings(moduleName := "finch-argonaut")
.settings(allSettings)
.settings(libraryDependencies += "io.argonaut" %% "argonaut" % "6.1")
.settings(libraryDependencies ++= Seq(
"io.argonaut" %% "argonaut" % "6.1",
"org.spire-math" %% "jawn-parser" % "0.8.3",
"org.spire-math" %% "jawn-argonaut" % "0.8.3"
))
.dependsOn(core, jsonTest % "test")

lazy val jackson = project
Expand Down

0 comments on commit d2316b6

Please sign in to comment.