Skip to content

Commit

Permalink
Merge branch 'release/0.6.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Coveney committed Feb 4, 2014
2 parents 76e68b6 + 73d9090 commit d0cbd28
Show file tree
Hide file tree
Showing 10 changed files with 650 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Bijection #

### 0.6.1
* Add the sbt version helper script: https://github.com/twitter/bijection/pull/160
* Added Json4s Injections: https://github.com/twitter/bijection/pull/157
* Added url encoded String Inection: https://github.com/twitter/bijection/pull/156
* Added bytes2bytesWritable bijection: https://github.com/twitter/bijection/pull/154
* Update README.md: https://github.com/twitter/bijection/pull/153

### 0.6.0
* Update Avro: https://github.com/twitter/bijection/pull/147/files
* Fix a thread-safety bug with collection Bufferables: https://github.com/twitter/bijection/pull/150
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Discussion occurs primarily on the [Bijection mailing list](https://groups.googl

## Maven

Bijection modules are available on maven central. The current groupid and version for all modules is, respectively, `"com.twitter"` and `0.6.0`.
Bijection modules are available on maven central. The current groupid and version for all modules is, respectively, `"com.twitter"` and `0.6.1`.

Current published artifacts are

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ limitations under the License.

package com.twitter.bijection

import java.net.URL
import java.net.{URLDecoder, URLEncoder, URL}
import java.util.UUID

import scala.annotation.tailrec
import scala.collection.generic.CanBuildFrom

import com.twitter.bijection.Inversion.attempt
import scala.util.Try

trait StringInjections extends NumericInjections {
implicit val utf8: Injection[String, Array[Byte]] = withEncoding("UTF-8")
Expand All @@ -46,6 +47,14 @@ trait StringInjections extends NumericInjections {
def apply(uuid: UUID) = uuid.toString
override def invert(s: String) = attempt(s)(UUID.fromString(_))
}

case class URLEncodedString(encodedString: String)

implicit val string2UrlEncodedString: Injection[String, URLEncodedString] = new AbstractInjection[String, URLEncodedString] {
override def apply(a: String): URLEncodedString = URLEncodedString(URLEncoder.encode(a, "UTF-8"))

override def invert(b: URLEncodedString): Try[String] = attempt(b)(s => URLDecoder.decode(s.encodedString, "UTF-8"))
}
}

object StringCodec extends StringInjections
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ object HBaseBijections {
implicit lazy val short2BytesWritable = ImmutableBytesWritableBijection[Short]
implicit lazy val boolean2BytesWritable = ImmutableBytesWritableBijection[Boolean]
implicit lazy val bigDecimal2BytesWritable = ImmutableBytesWritableBijection[BigDecimal]
implicit lazy val bytes2BytesWritable = new AbstractBijection[Array[Byte], ImmutableBytesWritable] {
override def apply(a: Array[Byte]): ImmutableBytesWritable = new ImmutableBytesWritable(a)

override def invert(b: ImmutableBytesWritable): Array[Byte] = b.get()
}

@implicitNotFound(msg = "Cannot find Bijection type class between ${T} and [Array[Byte] @@ Rep[${T}]")
object ImmutableBytesWritableBijection {
def apply[T](implicit bijection: Bijection[T, Array[Byte] @@ Rep[T]]) = new ImmutableBytesWritableBijection[T](bijection)
}
Expand Down
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])
}
}
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)
}
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
}
10 changes: 9 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ object BijectionBuild extends Build {
def youngestForwardCompatible(subProj: String) =
Some(subProj)
.filterNot(unreleasedModules.contains(_))
.map { s => "com.twitter" % ("bijection-" + s + "_2.9.3") % "0.5.4" }
.map { s => "com.twitter" % ("bijection-" + s + "_2.9.3") % "0.6.0" }

def osgiExportAll(packs: String*) =
OsgiKeys.exportPackage := packs.map(_ + ".*;version=${Bundle-Version}")
Expand Down Expand Up @@ -237,5 +237,13 @@ object BijectionBuild extends Build {
)
).dependsOn(bijectionCore % "test->test;compile->compile")

lazy val bijectionJson4s = module("json4s").settings(
osgiExportAll("com.twitter.bijection.json4s"),
libraryDependencies ++= Seq(
"org.json4s" %% "json4s-native" % "3.2.6",
"org.json4s" %% "json4s-ext" % "3.2.6"
)
).dependsOn(bijectionCore % "test->test;compile->compile")


}
Loading

0 comments on commit d0cbd28

Please sign in to comment.