Anyone else using Avro and avro4s to serialize persistence events? I was
able to get the SerializerWithStringManifest below working (with generic
serialize and deserialize methods). Should I be extending something other
than SerializerWithStringManifest, given that Avro on its own helps with
schema evolution?

Note: avro4s seems very promising, although I did run into this limitation
https://github.com/sksamuel/avro4s/issues/75

class PersistenceEventsAvroSerializer extends SerializerWithStringManifest {

  val FooManifest = "Foo"

  val BarManifest = "Bar"

  def identifier = 1234567

 override def manifest(obj: AnyRef): String =

    obj match {

      case _: Bar => BarManifest

      case _: Foo => FooManifest

    }

 override def toBinary(obj: AnyRef): Array[Byte] = {

   obj match {

      case foo: Foo => serialize[Foo](foo)

      case bsr: Bar => serialize[Bar](bar)

    }

  }

 override def fromBinary(bytes: Array[Byte], manifest: String): AnyRef = {

    manifest match {

      case FooManifest => deserialize[Foo](bytes)

      case BarManifest => deserialize[Bar](bytes)

    }

  }

  private def serialize[T](data: T)(implicit s: SchemaFor[T], r:
ToRecord[T]): Array[Byte] = {

    val baos = new ByteArrayOutputStream

    val output = AvroOutputStream.binary[T](baos)

    output.write(data)

    output.close()

    val result = baos.toByteArray()

    result

  }


  private def deserialize[T](data: Array[Byte])(implicit s: SchemaFor[T],
r: FromRecord[T]): T = {


    val input = AvroInputStream.binary[T](data)

    val result: T = if (input.iterator.isEmpty) {

      ??? // TODO Check

    } else {

      input.iterator.toSeq.head

    }

    result

  }


}

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to