diff --git a/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java b/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java index 76ae90f..2d86dbc 100644 --- a/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java +++ b/converter/src/main/java/tech/allegro/schema/json2avro/converter/JsonGenericRecordReader.java @@ -61,24 +61,31 @@ public GenericData.Record read(Map json, Schema schema) { } } - private GenericData.Record readRecord(Map json, Schema schema, Deque path) { - final GenericRecordBuilder record = new GenericRecordBuilder(schema); - final Map fieldAliasMap = new HashMap<>(1); + private static Map getFieldAliasMap(final Schema schema) { + final Map fieldAliasMap = new HashMap<>(1); for (Field field: schema.getFields()) { for (String alias: field.aliases()) { fieldAliasMap.put(alias, field); } } + return fieldAliasMap; + } + + private GenericData.Record readRecord(Map json, Schema schema, Deque path) { + final GenericRecordBuilder record = new GenericRecordBuilder(schema); json.entrySet().forEach(entry -> { final String entryKey = entry.getKey(); final Field field = schema.getField(entryKey); if (field != null) { record.set(field, read(field, field.schema(), entry.getValue(), path, false)); - } else if (fieldAliasMap.containsKey(entryKey)) { - final Field aliasedField = fieldAliasMap.get(entryKey); - record.set(aliasedField, read(aliasedField, aliasedField.schema(), entry.getValue(), path, false)); - } else if (unknownFieldListener != null) { - unknownFieldListener.onUnknownField(entry.getKey(), entry.getValue(), PathsPrinter.print(path, entry.getKey())); + } else { + final Map fieldAliasMap = getFieldAliasMap(schema); + if (fieldAliasMap.containsKey(entryKey)) { + final Field aliasedField = fieldAliasMap.get(entryKey); + record.set(aliasedField, read(aliasedField, aliasedField.schema(), entry.getValue(), path, false)); + } else if (unknownFieldListener != null) { + unknownFieldListener.onUnknownField(entry.getKey(), entry.getValue(), PathsPrinter.print(path, entry.getKey())); + } } }); return record.build();