-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
128 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
finch-jawn/src/main/scala/io/finch/json/jawn/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters