title: How to serialise a Scala case class with Avro?
body: <p>Following up on this question: <a href="https://stackoverflow.com/q/38936361/420892">Avro serialisation cast error in Scala</a> </p>
  
  <p>What is the best way to serialise a Scala <code>case</code> class with Avro?</p>
  
  <p>Here is what I'm doing now:</p>
  
  <pre><code> def serializeSubmapRecord(record: MyRecord): Array[Byte] = {
      val out = new ByteArrayOutputStream()
      val encoder = EncoderFactory.get.binaryEncoder(out, null)
      val writer = new GenericDatumWriter[GenericRecord](avro_schema)
      val r = new GenericData.Record(avro_schema);
      r.put("my_number", 1);
      writer.write(r, encoder)
      encoder.flush
      out.close
      out.toByteArray
    }
  </code></pre>
  
  <p>where the Avro schema is</p>
  
  <pre><code>{"namespace": "",
    "type": "record",
    "name": "MyRecord",
    "fields": [
      {"name": "my_number", "type": "int"}
    ]
  }
  </code></pre>
  
  <p>However I would like to have something like this:</p>
  
  <pre><code>case class MyRecord(my_number: Int)
  val record = new MyRecord(1)
  
  def serializeSubmapRecord(record: MyRecord): Array[Byte] = {
  val out = new ByteArrayOutputStream()
  val encoder = EncoderFactory.get.binaryEncoder(out, null)
  val writer = new GenericDatumWriter[MyRecord](avro_schema)
  writer.write(record, encoder)
  encoder.flush
  out.close
  out.toByteArray
  </code></pre>
  
  <p>}</p>
  
  <p>The last bit of code gives the exception of the linked question. What am I doing wrong?</p>