Source:
public Object toAvro(Schema schema, Property input) { switch (schema.getType()) { case NULL: if (input.getValue() == null) { return null; } throw new NonNullValueException(); case UNION: for (Schema s : schema.getTypes()) { try { return service.toAvro(s, input); } catch (NonNullValueException e) { } } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); case RECORD: if (input.isComplex()) { GenericRecord record = new GenericData.Record(schema); for (Field f : schema.getFields()) { record.put(f.name(), service.toAvro(f.schema(), input.get(service.decodeName(f.name())))); } return record; } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); case ARRAY: if (input.getType().isListType()) { Collection<Object> objects; if (((ListType) input.getType()).isArray()) { objects = Arrays.asList((Object[]) input.getValue()); } else { ListProperty list = (ListProperty) input; objects = new ArrayList<>(list.size()); for (Property p : list) { objects.add(service.toAvro(schema.getElementType(), p)); } } return new GenericData.Array<>(schema, objects); } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); case INT: case FLOAT: case STRING: case DOUBLE: case BOOLEAN: if (input.isScalar()) { return input.getValue(); } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); case BYTES: System.err.println("encode byte"); return null; case LONG: if (input.isScalar()) { if ("timestamp-millis".equals(getLogicalType(schema))) { GregorianCalendar cal = (GregorianCalendar) input.getValue(); return cal.toInstant().toEpochMilli(); } return input.getValue(); } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); default: throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); } }

Target:
public Object toAvro(Schema schema, Property input) { switch (schema.getType()) { case NULL: if (input.getValue() == null) { return null; } throw new NonNullValueException(); case UNION: for (Schema s : schema.getTypes()) { try { return service.toAvro(s, input); } catch (NonNullValueException e) { } } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); case RECORD: if (input.isComplex()) { GenericRecord record = new GenericData.Record(schema); for (Field f : schema.getFields()) { record.put(f.name(), service.toAvro(f.schema(), input.get(service.decodeName(f.name())))); } return record; } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); case ARRAY: if (input.getType().isListType()) { Collection<Object> objects; if (((ListType) input.getType()).isArray()) { objects = Arrays.asList((Object[]) input.getValue()); } else { ListProperty list = (ListProperty) input; objects = list.stream() .map(p -> service.toAvro(schema.getElementType(), p)) .collect(Collectors.toList()); } return new GenericData.Array<>(schema, objects); } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); case INT: case FLOAT: case STRING: case DOUBLE: case BOOLEAN: if (input.isScalar()) { return input.getValue(); } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); case LONG: if (input.isScalar()) { if (TIMESTAMP_MILLIS.equals(getLogicalType(schema))) { GregorianCalendar cal = (GregorianCalendar) input.getValue(); return cal.toInstant().toEpochMilli(); } return input.getValue(); } throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); default: throw new RuntimeServiceException(CANNOT_MAP_TO + schema.getType()); } }