Created
November 5, 2013 17:34
-
-
Save maliqq/7322901 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
final val TypeField = "$type" | |
final val ObjectField = "$object" | |
def encode[T <: Message](msg: T): Array[Byte] = { | |
val schema: protostuff.Schema[T] = msg.schema.asInstanceOf[protostuff.Schema[T]] | |
val out = new java.io.ByteArrayOutputStream | |
val context = new org.codehaus.jackson.io.IOContext(protostuff.JsonIOUtil.DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false); | |
val generator = protostuff.JsonIOUtil.newJsonGenerator(out, context.allocWriteEncodingBuffer) | |
generator.writeStartObject | |
generator.writeStringField(TypeField, schema.messageName) | |
generator.writeFieldName(ObjectField) | |
try { | |
protostuff.JsonIOUtil.writeTo(generator, msg, schema, false) | |
} finally { | |
generator.writeEndObject | |
generator.close | |
} | |
out.toByteArray | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment