I has a problems with generic version toByteArray / fromByteArray functions for Avro4s
I found this Avro serialization with generic type issue
this is works:
def fromByteArray[A: SchemaFor : FromRecord](bytes: Array[Byte]): Option[A] = {
val in: ByteArrayInputStream = new ByteArrayInputStream(bytes)
val input: AvroBinaryInputStream[A] = AvroInputStream.binary[A](in)
val result: Option[A] = input.iterator.toSeq.headOption
input.close()
result
}
but this is not!
def toByteArray[T : SchemaFor : FromRecord](obj: T): Array[Byte] = {
val byteArrayStream = new ByteArrayOutputStream()
val output = AvroOutputStream.binary[T](byteArrayStream)
output.write(obj)
output.close()
byteArrayStream.toByteArray
}
it throws compile exception:
Error:(48, 44) could not find implicit value for evidence parameter of type com.sksamuel.avro4s.ToRecord[T] val output = AvroOutputStream.binaryT Error:(48, 44) not enough arguments for method binary: (implicit evidence$23: com.sksamuel.avro4s.SchemaFor[T], implicit evidence$24: com.sksamuel.avro4s.ToRecord[T])com.sksamuel.avro4s.AvroBinaryOutputStream[T]. Unspecified value parameter evidence$24. val output = AvroOutputStream.binaryT
what i am doing wrong?
upd stupid mistake
def toByteArray[T : SchemaFor : FromRecord](obj: T): Array[Byte] ...
must be
def createByteArray[T : SchemaFor : ToRecord](obj: T): Array[Byte] ...
:D
on problem not solved yet
if one of the field of class is AnyRef
val output = AvroOutputStream.binary[T](byteArrayStream)
failed to compile with : Could not find implicit SchemaFor ...