completed scala binary serialization'
This commit is contained in:
parent
2aca074b84
commit
7473afd9c4
10 changed files with 997 additions and 876 deletions
|
|
@ -28,10 +28,43 @@ trait Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Serialization protocols.
|
||||
*
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
object Serializable {
|
||||
|
||||
/**
|
||||
* Example on how to use the SBinary serialization protocol:
|
||||
* <pre>
|
||||
* case class User(val usernamePassword: Tuple2[String, String],
|
||||
* val email: String,
|
||||
* val age: Int)
|
||||
* extends Serializable.SBinary[User] {
|
||||
* def this() = this(null, null, 0)
|
||||
* import sbinary.DefaultProtocol._
|
||||
* implicit object UserFormat extends Format[User] {
|
||||
* def reads(in : Input) = User(
|
||||
* read[Tuple2[String, String]](in),
|
||||
* read[String](in),
|
||||
* read[Int](in))
|
||||
* def writes(out: Output, value: User) = {
|
||||
* write[Tuple2[String, String]](out, value.usernamePassword)
|
||||
* write[String](out, value.email)
|
||||
* write[Int](out, value.age)
|
||||
* }
|
||||
* }
|
||||
* def fromBytes(bytes: Array[Byte]) = fromByteArray[User](bytes)
|
||||
* def toBytes: Array[Byte] = toByteArray(this)
|
||||
* }
|
||||
* </pre>
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
trait SBinary[T <: AnyRef] extends Serializable {
|
||||
def fromBytes(bytes: Array[Byte]): T
|
||||
def toBytes: Array[Byte]
|
||||
}
|
||||
|
||||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
|
|
@ -43,7 +76,7 @@ object Serializable {
|
|||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
trait JavaJSON[T] extends JSON[T]{
|
||||
abstract class JavaJSON[T] extends JSON[T]{
|
||||
private val mapper = new ObjectMapper
|
||||
|
||||
def toJSON: String = {
|
||||
|
|
@ -77,17 +110,4 @@ object Serializable {
|
|||
def toBytes: Array[Byte]
|
||||
def getSchema: Message
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* import sbinary.DefaultProtocol._
|
||||
* def fromBytes(bytes: Array[Byte]) = fromByteArray[String](bytes)
|
||||
* def toBytes: Array[Byte] = toByteArray(body)
|
||||
* </pre>
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
trait SBinary extends Serializable {
|
||||
def fromBytes(bytes: Array[Byte])
|
||||
def toBytes: Array[Byte]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue