added scala-json lib

This commit is contained in:
jboner 2009-07-21 13:59:12 +02:00
parent f26110e55c
commit a3fac4338f
3 changed files with 79 additions and 23 deletions

View 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")
}
}
}

View file

@ -5,15 +5,17 @@
package se.scalablesolutions.akka.kernel.util
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&#233;r</a>
*/
trait Serializer {
def deepClone[T <: AnyRef](obj: T): T = in(out(obj), Some(obj.getClass.asInstanceOf[Class[T]])).asInstanceOf[T]
trait Serializer {
def deepClone[T <: AnyRef](obj: T): T
def out(obj: AnyRef): Array[Byte]
def in(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef
}
@ -21,6 +23,8 @@ trait Serializer {
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
object JavaSerializationSerializer extends Serializer {
def deepClone[T <: AnyRef](obj: T): T = in(out(obj), None).asInstanceOf[T]
def out(obj: AnyRef): Array[Byte] = {
val bos = new ByteArrayOutputStream
val out = new ObjectOutputStream(bos)
@ -41,12 +45,11 @@ object JavaSerializationSerializer extends Serializer {
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
object JavaJSONSerializer extends Serializer {
import org.codehaus.jackson.map.ObjectMapper
private val json = new ObjectMapper
private val json = new org.codehaus.jackson.map.ObjectMapper
def deepClone[T <: AnyRef](obj: T): T = in(out(obj), Some(obj.getClass)).asInstanceOf[T]
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 out = new ObjectOutputStream(bos)
json.writeValue(out, obj)
@ -67,24 +70,26 @@ object JavaJSONSerializer extends Serializer {
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
object ScalaJSONSerializer extends Serializer {
import org.codehaus.jackson.map.ObjectMapper
private val json = new ObjectMapper
def deepClone[T <: AnyRef](obj: T): T = in(out(obj), None).asInstanceOf[T]
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 out = new ObjectOutputStream(bos)
json.writeValue(out, obj)
out.close
bos.toByteArray
Json.build(obj).toString.getBytes("UTF-8")
}
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")
val in = new ObjectInputStream(new ByteArrayInputStream(bytes))
val obj = json.readValue(in, clazz.get).asInstanceOf[AnyRef]
in.close
obj
Json.parse(new String(bytes, "UTF-8")).asInstanceOf[AnyRef]
}
}
/**
* @author <a href="http://jonasboner.com">Jonas Bon&#233;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

Binary file not shown.