-
Notifications
You must be signed in to change notification settings - Fork 123
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
10 changed files
with
650 additions
and
5 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
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
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
60 changes: 60 additions & 0 deletions
60
bijection-json4s/src/main/scala/com/twitter/bijection/json4s/Json4sInjections.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,60 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.twitter.bijection.json4s | ||
|
||
import org.json4s._ | ||
import com.twitter.bijection.{Injection, AbstractInjection} | ||
import org.json4s.native.JsonMethods._ | ||
import scala.util.Try | ||
import com.twitter.bijection.Inversion._ | ||
import org.json4s.native.Serialization._ | ||
|
||
/** | ||
* @author Mansur Ashraf | ||
* @since 1/10/14 | ||
*/ | ||
object Json4sInjections { | ||
|
||
/** | ||
* JValue to Json Injection | ||
*/ | ||
implicit val jvalue2Json: Injection[JValue, String] = new AbstractInjection[JValue, String] { | ||
override def apply(a: JValue): String = compact(render(a)) | ||
|
||
override def invert(b: String): Try[JValue] = attempt(b)(parse(_)) | ||
} | ||
|
||
/** | ||
* Case Class to Json Injection | ||
* @tparam A Case Class | ||
* @return Json String | ||
*/ | ||
implicit def caseClass2Json[A <: AnyRef](implicit mf:Manifest[A],fmt:Formats): Injection[A, String] = new AbstractInjection[A, String] { | ||
override def apply(a: A): String = write(a) | ||
|
||
override def invert(b: String): Try[A] = attempt(b)(read[A]) | ||
} | ||
|
||
/** | ||
* Case Class to JValue Injection | ||
* @tparam A Case Class | ||
* @return JValue | ||
*/ | ||
implicit def caseClass2JValue[A <: AnyRef ](implicit mf:Manifest[A],fmt:Formats): Injection[A, JValue] = new AbstractInjection[A, JValue] { | ||
override def apply(a: A): JValue = Extraction.decompose(a) | ||
|
||
override def invert(b: JValue): Try[A] = attempt(b)(_.extract[A]) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
bijection-json4s/src/main/scala/com/twitter/bijection/json4s/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,25 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.twitter.bijection | ||
|
||
import org.json4s.{NoTypeHints, native} | ||
|
||
/** | ||
* @author Mansur Ashraf | ||
* @since 1/18/14 | ||
*/ | ||
package object json4s { | ||
implicit val formats = native.Serialization.formats(NoTypeHints) | ||
} |
57 changes: 57 additions & 0 deletions
57
bijection-json4s/src/test/scala/com/twitter/bijection/json4s/Json4sInjectionLaws.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,57 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package com.twitter.bijection.json4s | ||
|
||
import org.scalacheck.Properties | ||
import com.twitter.bijection.{Injection, BaseProperties} | ||
import Json4sInjections._ | ||
import org.json4s.JsonAST._ | ||
import org.json4s.JsonAST.JString | ||
|
||
/** | ||
* @author Mansur Ashraf | ||
* @since 1/10/14 | ||
*/ | ||
object Json4sInjectionLaws extends Properties("Json4sInjection") | ||
with BaseProperties { | ||
case class Twit(name: String, id: Int, id_str: String, indices: List[Int], screen_name: String) | ||
|
||
def createTwit(i: (String, Int, String, List[Int], String)): Twit = Twit(i._1, i._2, i._3, i._4, i._5) | ||
|
||
implicit val testCaseClassToJson = arbitraryViaFn { | ||
in: (String, Int, String, List[Int], String) => createTwit(in) | ||
} | ||
|
||
implicit val testJValueToJson = arbitraryViaFn[(String, Int, String, List[Int], String),JValue] { | ||
in: (String, Int, String, List[Int], String) => JObject( | ||
List( | ||
JField("name", JString(in._1)), | ||
JField("id", JInt(in._2)), | ||
JField("id_String", JString(in._3)), | ||
JField("indices", JArray(in._4.map(JInt(_)))), | ||
JField("screen_name", JString(in._5)) | ||
)) | ||
} | ||
|
||
def roundTripCaseClassToJson(implicit inj: Injection[Twit, String], mn: Manifest[Twit]) = isLooseInjection[Twit, String] | ||
|
||
def roundTripCaseClassToJValue(implicit inj: Injection[Twit, JValue], mn: Manifest[Twit]) = isLooseInjection[Twit, JValue] | ||
|
||
def roundTripJValueToString(implicit inj: Injection[JValue, String]) = isLooseInjection[JValue, String] | ||
|
||
property("round trip Case Class to Json") = roundTripCaseClassToJson | ||
property("round trip Case Class to JValue") = roundTripCaseClassToJValue | ||
property("round trip JValue to String") = roundTripJValueToString | ||
} |
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
Oops, something went wrong.