com.liferay.batch.engine.internal.reader.JSONBatchEngineImportTaskItemReaderImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.batch.engine.service
Show all versions of com.liferay.batch.engine.service
Liferay Batch Engine Service
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.batch.engine.internal.reader;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* @author Ivica Cardic
*/
public class JSONBatchEngineImportTaskItemReaderImpl
implements BatchEngineImportTaskItemReader {
public JSONBatchEngineImportTaskItemReaderImpl(
List includeFieldNames, InputStream inputStream)
throws IOException {
if (!includeFieldNames.isEmpty()) {
_fieldNameFilter = new FieldNameFilterFunction(includeFieldNames);
}
_inputStream = inputStream;
_jsonParser = _jsonFactory.createParser(_inputStream);
JsonToken jsonToken = _jsonParser.nextToken();
if (jsonToken != JsonToken.START_ARRAY) {
throw new IllegalArgumentException(
"Provided stream is not a JSON array");
}
}
@Override
public void close() throws IOException {
_inputStream.close();
_jsonParser.close();
}
@Override
public Map read() throws Exception {
if (_jsonParser.nextToken() == JsonToken.START_OBJECT) {
return _fieldNameFilter.apply(
_objectMapper.readValue(
_jsonParser,
new TypeReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy