title: Avro: deserialize json - schema with optional fields
body: <p>There are a lot of questions and answers on stackoverflow on the subject, but no one that helps.</p>
  
  <p>I have a schema with optional value:</p>
  
  <pre><code>{
   "type" : "record",
   "name" : "UserSessionEvent",
   "namespace" : "events",
   "fields" : [ {
     "name" : "username",
     "type" : "string"
   }, {
     "name" : "errorData",
     "type" : [ "null", "string" ],
     "default" : null
   }]
  }
  </code></pre>
  
  <p>And I'm trying deserialize json w/o this field:</p>
  
  <pre><code>{
   "username" : "2271AE67-34DE-4B43-8839-07216C5D10E1",
   "errorData" : { "string":"070226AC-9B91-47CE-85FE-15AA17972298"}
  }
  </code></pre>
  
  <p>using code:</p>
  
  <pre><code>val reader = new GenericDatumReader[GenericRecord](schema)
  val decoder = DecoderFactory.get().jsonDecoder(schema, json)
  reader.read(null, decoder)
  </code></pre>
  
  <p>and I got: <code>org.apache.avro.AvroTypeException: Expected field name not found: errorData</code></p>
  
  <p>The only way that works is json</p>
  
  <pre><code>{
   "username" : "2271AE67-34DE-4B43-8839-07216C5D10E1",
   "errorData" : null
  </code></pre>
  
  <p>}</p>
  
  <p>Is there a way to deserialize json w/o this field?</p>
  
  <p>Another question: when this field is here, I should write</p>
  
  <pre><code>{
   "username" : "2271AE67-34DE-4B43-8839-07216C5D10E1",
   "errorData" : { "string":"070226AC-9B91-47CE-85FE-15AA17972298"}
  </code></pre>
  
  <p>}</p>
  
  <p>Is there a way to deserialize a "normal" json:</p>
  
  <pre><code>{
   "username" : "2271AE67-34DE-4B43-8839-07216C5D10E1",
   "errorData" : "070226AC-9B91-47CE-85FE-15AA17972298"
  }
  </code></pre>
  
  <p>?</p>