diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/transform/JsonLineAggregator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/transform/JsonLineAggregator.java new file mode 100644 index 0000000000..aa157a6680 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/transform/JsonLineAggregator.java @@ -0,0 +1,46 @@ +/* + * Copyright 2006-2026 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.infrastructure.item.file.transform; + +import org.springframework.batch.infrastructure.item.file.mapping.JsonLineMapper; +import org.springframework.batch.infrastructure.item.json.JsonObjectMarshaller; + +/** + * A {@link LineAggregator} implementation that converts objects into + * JSON Lines. + * + * @author Yanming Zhou + * @see JsonLineMapper + */ +public class JsonLineAggregator implements LineAggregator { + + private final JsonObjectMarshaller jsonObjectMarshaller; + + public JsonLineAggregator(JsonObjectMarshaller jsonObjectMarshaller) { + this.jsonObjectMarshaller = jsonObjectMarshaller; + } + + /** + * Marshal the provided item into JSON string. + * @param item value to be converted + * @return JSON string + */ + @Override + public String aggregate(T item) { + return jsonObjectMarshaller.marshal(item); + } + +} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/file/transform/JsonLineAggregatorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/file/transform/JsonLineAggregatorTests.java new file mode 100644 index 0000000000..5005e3844e --- /dev/null +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/file/transform/JsonLineAggregatorTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2006-2026 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.infrastructure.item.file.transform; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.batch.infrastructure.item.json.JacksonJsonObjectMarshaller; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Yanming Zhou + * + */ +class JsonLineAggregatorTests { + + private JsonLineAggregator aggregator; + + @BeforeEach + void setup() { + this.aggregator = new JsonLineAggregator<>(new JacksonJsonObjectMarshaller<>()); + } + + @Test + void testAggregate() { + assertEquals(""" + {"name":"foo","email":"bar@example.com","introduction":"I'm\\npowerful\\nman"}""", + aggregator.aggregate(new User("foo", "bar@example.com", """ + I'm + powerful + man"""))); + } + + record User(String name, String email, String introduction) { + + } + +}