1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.item.file.separator; |
18 | |
19 | import org.springframework.util.StringUtils; |
20 | |
21 | /** |
22 | * JSON-based record separator. Waits for a valid JSON object before returning a |
23 | * complete line. A valid object has balanced braces ({}), possibly nested, and |
24 | * ends with a closing brace. This separator can be used to split a stream into |
25 | * JSON objects, even if those objects are spread over multiple lines, e.g. |
26 | * |
27 | * <pre> |
28 | * {"foo": "bar", |
29 | * "value": { "spam": 2 }} |
30 | * {"foo": "rab", |
31 | * "value": { "spam": 3, "foo": "bar" }} |
32 | * </pre> |
33 | * |
34 | * @author Dave Syer |
35 | * |
36 | */ |
37 | public class JsonRecordSeparatorPolicy extends SimpleRecordSeparatorPolicy { |
38 | |
39 | /** |
40 | * True if the line can be parsed to a JSON object. |
41 | * |
42 | * @see RecordSeparatorPolicy#isEndOfRecord(String) |
43 | */ |
44 | @Override |
45 | public boolean isEndOfRecord(String line) { |
46 | return StringUtils.countOccurrencesOf(line, "{") == StringUtils.countOccurrencesOf(line, "}") |
47 | && line.trim().endsWith("}"); |
48 | } |
49 | |
50 | } |