@Test public void testFormatNullableBoolean() { byte[] data = Json.createObjectNode() .put("test_some", true).set("test_none", NullNode.getInstance()) .toString().getBytes(StandardCharsets.UTF_8); PubsubMessage message = new PubsubMessage(data, Collections.emptyMap()); Schema schema = SchemaBuilder .record("root").fields() .name("test_none").type().unionOf().nullType().and().booleanType().endUnion().noDefault() .name("test_some").type().unionOf().nullType().and().booleanType().endUnion().noDefault() .endRecord(); PubsubMessageRecordFormatter formatter = new PubsubMessageRecordFormatter(); GenericRecord record = formatter.formatRecord(message, schema); assertEquals(null, record.get("test_none")); assertEquals(true, record.get("test_some")); }
@Override public GenericRecord formatRecord(PubsubMessage element, Schema schema) { GenericRecord result = null; try { InputStream in = new ByteArrayInputStream(element.getPayload()); DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema); Decoder decoder = new GuidedJsonDecoder(schema, in); result = reader.read(null, decoder); } catch (IOException e) { throw new UncheckedIOException(e); } catch (AvroTypeException e) { throw e; } return result; }
PubsubMessageRecordFormatter implements RecordFormatter<PubsubMessage> { @Override public GenericRecord formatRecord(PubsubMessage element, Schema schema) { GenericRecord result = null; try { InputStream in = new ByteArrayInputStream(element.getPayload()); DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema); Decoder decoder = new GuidedJsonDecoder(schema, in); result = reader.read(null, decoder); } catch (IOException e) { throw new UncheckedIOException(e); } catch (AvroTypeException e) { throw e; } return result; } }
PubsubMessageRecordFormatter implements RecordFormatter<PubsubMessage> { @Override public GenericRecord formatRecord(PubsubMessage element, Schema schema) { GenericRecord result = null; try { InputStream in = new ByteArrayInputStream(element.getPayload()); DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema); Decoder decoder = new GuidedJsonDecoder(schema, in); result = reader.read(null, decoder); } catch (IOException e) { throw new UncheckedIOException(e); } catch (AvroTypeException e) { throw e; } return result; }  }
PubsubMessageRecordFormatter implements RecordFormatter<PubsubMessage> { @Override public GenericRecord formatRecord(PubsubMessage element, Schema schema) { GenericRecord result = null; try { InputStream in = new ByteArrayInputStream(element.getPayload()); DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema); Decoder decoder = new GuidedJsonDecoder(schema, in); result = reader.read(null, decoder); } catch (IOException e) { throw new UncheckedIOException(e); } catch (AvroTypeException e) { throw e; } return result; }  @Override GenericRecord formatRecord(PubsubMessage element, Schema schema); }
PubsubMessageRecordFormatter implements RecordFormatter<PubsubMessage> { @Override public GenericRecord formatRecord(PubsubMessage element, Schema schema) { GenericRecord result = null; try { InputStream in = new ByteArrayInputStream(element.getPayload()); DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema); Decoder decoder = new GuidedJsonDecoder(schema, in); result = reader.read(null, decoder); } catch (IOException e) { throw new UncheckedIOException(e); } catch (AvroTypeException e) { throw e; } return result; }  @Override GenericRecord formatRecord(PubsubMessage element, Schema schema);  }