Use one factory in json reader (#11999)

This commit is contained in:
Karan Kumar 2021-12-01 16:17:48 +05:30 committed by GitHub
parent 11746b8536
commit ffa553593f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 2 deletions

View File

@ -65,6 +65,7 @@ public class JsonReader extends IntermediateRowParsingReader<String>
private final ObjectMapper mapper;
private final InputEntity source;
private final InputRowSchema inputRowSchema;
private final JsonFactory jsonFactory;
JsonReader(
InputRowSchema inputRowSchema,
@ -78,6 +79,7 @@ public class JsonReader extends IntermediateRowParsingReader<String>
this.source = source;
this.flattener = ObjectFlatteners.create(flattenSpec, new JSONFlattenerMaker(keepNullColumns));
this.mapper = mapper;
this.jsonFactory = new JsonFactory();
}
@Override
@ -92,7 +94,7 @@ public class JsonReader extends IntermediateRowParsingReader<String>
protected List<InputRow> parseInputRows(String intermediateRow) throws IOException, ParseException
{
final List<InputRow> inputRows;
try (JsonParser parser = new JsonFactory().createParser(intermediateRow)) {
try (JsonParser parser = jsonFactory.createParser(intermediateRow)) {
final MappingIterator<JsonNode> delegate = mapper.readValues(parser, JsonNode.class);
inputRows = FluentIterable.from(() -> delegate)
.transform(jsonNode -> MapInputRowParser.parse(inputRowSchema, flattener.flatten(jsonNode)))
@ -117,7 +119,7 @@ public class JsonReader extends IntermediateRowParsingReader<String>
@Override
protected List<Map<String, Object>> toMap(String intermediateRow) throws IOException
{
try (JsonParser parser = new JsonFactory().createParser(intermediateRow)) {
try (JsonParser parser = jsonFactory.createParser(intermediateRow)) {
final MappingIterator<Map> delegate = mapper.readValues(parser, Map.class);
return FluentIterable.from(() -> delegate)
.transform(map -> (Map<String, Object>) map)