Skip to content

Instantly share code, notes, and snippets.

@maliqq
Created November 5, 2013 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maliqq/7322901 to your computer and use it in GitHub Desktop.
Save maliqq/7322901 to your computer and use it in GitHub Desktop.
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