added scala-json lib
This commit is contained in:
parent
f26110e55c
commit
a3fac4338f
3 changed files with 79 additions and 23 deletions
51
kernel/src/main/scala/serialization/Serializable.scala
Normal file
51
kernel/src/main/scala/serialization/Serializable.scala
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
/**
|
||||||
|
* Copyright (C) 2009 Scalable Solutions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package se.scalablesolutions.akka.serialization
|
||||||
|
|
||||||
|
|
||||||
|
import com.twitter.commons.Json
|
||||||
|
import java.io.{StringWriter, ByteArrayOutputStream, ObjectOutputStream}
|
||||||
|
import reflect.Manifest
|
||||||
|
import sbinary.DefaultProtocol
|
||||||
|
|
||||||
|
object Serializable {
|
||||||
|
trait Protobuf {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
trait SBinary[T] extends DefaultProtocol {
|
||||||
|
def toBytes: Array[Byte] = toByteArray(this)
|
||||||
|
def getManifest: Manifest[T] = Manifest.singleType(this.asInstanceOf[T])
|
||||||
|
}
|
||||||
|
|
||||||
|
trait JavaJSON {
|
||||||
|
private val mapper = new org.codehaus.jackson.map.ObjectMapper
|
||||||
|
|
||||||
|
def toJSON: String = {
|
||||||
|
val out = new StringWriter
|
||||||
|
mapper.writeValue(out, obj)
|
||||||
|
out.close
|
||||||
|
out.toString
|
||||||
|
}
|
||||||
|
|
||||||
|
def toBytes: Array[Byte] = {
|
||||||
|
val bos = new ByteArrayOutputStream
|
||||||
|
val out = new ObjectOutputStream(bos)
|
||||||
|
mapper.writeValue(out, obj)
|
||||||
|
out.close
|
||||||
|
bos.toByteArray
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait ScalaJSON {
|
||||||
|
def toJSON: String = {
|
||||||
|
Json.build(obj).toString.getBytes("UTF-8")
|
||||||
|
}
|
||||||
|
|
||||||
|
def toBytes: Array[Byte] = {
|
||||||
|
Json.build(obj).toString.getBytes("UTF-8")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,15 +5,17 @@
|
||||||
package se.scalablesolutions.akka.kernel.util
|
package se.scalablesolutions.akka.kernel.util
|
||||||
|
|
||||||
import java.io.{ObjectOutputStream, ByteArrayOutputStream, ObjectInputStream, ByteArrayInputStream}
|
import java.io.{ObjectOutputStream, ByteArrayOutputStream, ObjectInputStream, ByteArrayInputStream}
|
||||||
|
import reflect.Manifest
|
||||||
|
import sbinary.DefaultProtocol
|
||||||
|
import org.codehaus.jackson.map.ObjectMapper
|
||||||
|
import com.twitter.commons.Json
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
*/
|
*/
|
||||||
trait Serializer {
|
trait Serializer {
|
||||||
def deepClone[T <: AnyRef](obj: T): T = in(out(obj), Some(obj.getClass.asInstanceOf[Class[T]])).asInstanceOf[T]
|
def deepClone[T <: AnyRef](obj: T): T
|
||||||
|
|
||||||
def out(obj: AnyRef): Array[Byte]
|
def out(obj: AnyRef): Array[Byte]
|
||||||
|
|
||||||
def in(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef
|
def in(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -21,6 +23,8 @@ trait Serializer {
|
||||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
*/
|
*/
|
||||||
object JavaSerializationSerializer extends Serializer {
|
object JavaSerializationSerializer extends Serializer {
|
||||||
|
def deepClone[T <: AnyRef](obj: T): T = in(out(obj), None).asInstanceOf[T]
|
||||||
|
|
||||||
def out(obj: AnyRef): Array[Byte] = {
|
def out(obj: AnyRef): Array[Byte] = {
|
||||||
val bos = new ByteArrayOutputStream
|
val bos = new ByteArrayOutputStream
|
||||||
val out = new ObjectOutputStream(bos)
|
val out = new ObjectOutputStream(bos)
|
||||||
|
|
@ -41,12 +45,11 @@ object JavaSerializationSerializer extends Serializer {
|
||||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
*/
|
*/
|
||||||
object JavaJSONSerializer extends Serializer {
|
object JavaJSONSerializer extends Serializer {
|
||||||
import org.codehaus.jackson.map.ObjectMapper
|
private val json = new org.codehaus.jackson.map.ObjectMapper
|
||||||
|
|
||||||
private val json = new ObjectMapper
|
def deepClone[T <: AnyRef](obj: T): T = in(out(obj), Some(obj.getClass)).asInstanceOf[T]
|
||||||
|
|
||||||
def out(obj: AnyRef): Array[Byte] = {
|
def out(obj: AnyRef): Array[Byte] = {
|
||||||
if (!json.canSerialize(obj.getClass)) throw new IllegalArgumentException("Can not serialize [" + obj + "] to JSON, please provide a JSON serializable object.")
|
|
||||||
val bos = new ByteArrayOutputStream
|
val bos = new ByteArrayOutputStream
|
||||||
val out = new ObjectOutputStream(bos)
|
val out = new ObjectOutputStream(bos)
|
||||||
json.writeValue(out, obj)
|
json.writeValue(out, obj)
|
||||||
|
|
@ -67,24 +70,26 @@ object JavaJSONSerializer extends Serializer {
|
||||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
*/
|
*/
|
||||||
object ScalaJSONSerializer extends Serializer {
|
object ScalaJSONSerializer extends Serializer {
|
||||||
import org.codehaus.jackson.map.ObjectMapper
|
def deepClone[T <: AnyRef](obj: T): T = in(out(obj), None).asInstanceOf[T]
|
||||||
|
|
||||||
private val json = new ObjectMapper
|
|
||||||
|
|
||||||
def out(obj: AnyRef): Array[Byte] = {
|
def out(obj: AnyRef): Array[Byte] = {
|
||||||
if (!json.canSerialize(obj.getClass)) throw new IllegalArgumentException("Can not serialize [" + obj + "] to JSON, please provide a JSON serializable object.")
|
Json.build(obj).toString.getBytes("UTF-8")
|
||||||
val bos = new ByteArrayOutputStream
|
|
||||||
val out = new ObjectOutputStream(bos)
|
|
||||||
json.writeValue(out, obj)
|
|
||||||
out.close
|
|
||||||
bos.toByteArray
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def in(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = {
|
def in(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = {
|
||||||
if (!clazz.isDefined) throw new IllegalArgumentException("Can't deserialize JSON to instance if no class is provided")
|
Json.parse(new String(bytes, "UTF-8")).asInstanceOf[AnyRef]
|
||||||
val in = new ObjectInputStream(new ByteArrayInputStream(bytes))
|
|
||||||
val obj = json.readValue(in, clazz.get).asInstanceOf[AnyRef]
|
|
||||||
in.close
|
|
||||||
obj
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||||
|
*/
|
||||||
|
object SBinarySerializer extends SBinarySerializer
|
||||||
|
trait SBinarySerializer extends DefaultProtocol {
|
||||||
|
def in[T](t : T)(implicit bin : Writes[T], m: Manifest[T]): Pair[Array[Byte], Manifest[T]] =
|
||||||
|
Pair(toByteArray(t), m)
|
||||||
|
|
||||||
|
def out[T](array : Array[Byte], m: Manifest[T])(implicit bin : Reads[T]) =
|
||||||
|
read[T](new ByteArrayInputStream(array))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
BIN
lib/scala-json-1.0.jar
Normal file
BIN
lib/scala-json-1.0.jar
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue