Skip to content

Commit

Permalink
Merge branch 'rpless-feature/jawn'
Browse files Browse the repository at this point in the history
  • Loading branch information
vkostyukov committed Dec 5, 2014
2 parents bb3f177 + 3c614e5 commit a99ec2e
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 10 deletions.
11 changes: 3 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
*.iml
/target/
/project/target/
/project/project/target/
/.idea/
/.idea_modules/
/finch-core/target/
/finch-json/target/
/finch-demo/target/
target/
.idea/
.idea_modules/
16 changes: 16 additions & 0 deletions finch-jawn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Finch Jawn
----------

This module provides support for using [Jawn](https://github.com/non/jawn) in Finch.

## Decoding Json

To decode a string with Jawn, you need to import `io.finch.json.jawn._` and define any Jawn `Facade` as an implicit val.
Using either `RequiredJsonBody` or `OptionalJsonBody` will take care of the rest.


## Encoding Json

To Encode a value from the Jawn ast (specifically a `JValue`), you need only import `io.finch.json.jawn._` and
the Encoder will be imported implicitly.

51 changes: 51 additions & 0 deletions finch-jawn/src/main/scala/io/finch/json/jawn/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2014, by Vladimir Kostyukov and Contributors.
*
* This file is a part of a Finch library that may be found at
*
* https://github.com/finagle/finch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributor(s):
* Ryan Plessner
*/

package io.finch.json

import _root_.jawn.ast.{CanonicalRenderer, JValue}
import _root_.jawn.{Facade, Parser}

/**
* This package provides support for use of the Jawn json parsing library in Finch.io.
*/
package object jawn {

/**
* @param facade The ''Facade'' that represents how jawn should parse json
* @tparam J The type of data returned by the ''Facade''
* @return Converts a jawn ''Facade'' into a ''DecodeJson''
*
*/
implicit def toJawnDecode[J](implicit facade: Facade[J]): DecodeJson[J] = new DecodeJson[J] {
def apply(json: String): Option[J] = Parser.parseFromString(json).toOption
}

/**
* The ''EncodeJawn'' object takes a ''JValue'' (part of Jawn's ast package) and
* returns the string representation of that value.
*/
implicit val EncodeJawn: EncodeJson[JValue] = new EncodeJson[JValue] {
def apply(json: JValue): String = CanonicalRenderer.render(json)
}
}
44 changes: 44 additions & 0 deletions finch-jawn/src/test/scala/io/finch/json/JawnSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2014, by Vladimir Kostyukov and Contributors.
*
* This file is a part of a Finch library that may be found at
*
* https://github.com/finagle/finch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributor(s):
* Ryan Plessner
*/

package io.finch.json

import io.finch.json.jawn._
import _root_.jawn.ast.{JawnFacade, JString, JObject}

import scala.collection.mutable
import org.scalatest.{Matchers, FlatSpec}

class JawnSpec extends FlatSpec with Matchers {
implicit val facade = JawnFacade
val str = "{\"name\": \"bob\" }"
val jsVal = JObject(mutable.Map("name" -> JString("bob")))

"A DecodeJawn" should "parse valid json into its ast" in {
toJawnDecode(facade)(str).foreach(v => v.shouldBe(jsVal))
}

"An EncodeJawn" should "render a valid JValue as a string" in {
EncodeJawn(jsVal) == str
}
}
1 change: 0 additions & 1 deletion finch-json/src/test/scala/io/finch/json/JsonSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class JsonSpec extends FlatSpec with Matchers {

it should "be able to read what it has wired" in {
val json = Json.obj("a.b" -> 10, "c.d" -> Json.arr("a", "b"))
println(json.toString)
Json.decode(json.toString) shouldBe Some(json)
}

Expand Down
15 changes: 14 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object Finch extends Build {
id = "finch",
base = file("."),
settings = allSettings
) aggregate(core, json, demo)
) aggregate(core, json, demo, jawn)

lazy val core = Project(
id = "finch-core",
Expand All @@ -65,4 +65,17 @@ object Finch extends Build {
base = file("finch-demo"),
settings = allSettings
) dependsOn(core, json)

lazy val jawnSettings = allSettings ++ Seq(
libraryDependencies ++= Seq(
"org.spire-math" %% "jawn-parser" % "0.7.0",
"org.spire-math" %% "jawn-ast" % "0.7.0"
)
)

lazy val jawn = Project(
id = "finch-jawn",
base = file("finch-jawn"),
settings = jawnSettings
) dependsOn(core)
}

0 comments on commit a99ec2e

Please sign in to comment.