IN PROGRESS - issue BATCH-1246: Add support for a JSON Reader from text files which are JSON formatted

Added line mapper and record separator using Jackson to parse the line up to a map.
This commit is contained in:
dsyer
2009-11-11 18:16:14 +00:00
parent f1f1b130e2
commit d8d75448c0
6 changed files with 168 additions and 3 deletions

View File

@@ -119,8 +119,8 @@ public class DefaultRecordSeparatorPolicy extends SimpleRecordSeparatorPolicy {
}
/**
* Determine if the current line (or buffered concatenation of lines)
* contains an unterminated quote, indicating that the record is continuing
* Determine if the current line (or buffered concatenation of lines) ends
* with the continuation marker, indicating that the record is continuing
* onto the next line.
*
* @param result

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2006-2007 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
*
* http://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.item.file.separator;
import org.springframework.util.StringUtils;
/**
* JSON-based record separator. Waits for a valid JSON object before returning a
* complete line. A valid object has balanced braces ({}), possibly nested, and
* ends with a closing brace. This separator can be used to split a stream into
* JSON objects, even if those objects are spread over multiple lines, e.g.
*
* <pre>
* {"foo": "bar",
* "value": { "spam": 2 }}
* {"foo": "rab",
* "value": { "spam": 3, "foo": "bar" }}
* </pre>
*
* @author Dave Syer
*
*/
public class JsonRecordSeparatorPolicy extends SimpleRecordSeparatorPolicy {
/**
* True if the line can be parsed to a JSON object.
*
* @see RecordSeparatorPolicy#isEndOfRecord(String)
*/
public boolean isEndOfRecord(String line) {
return StringUtils.countOccurrencesOf(line, "{") == StringUtils.countOccurrencesOf(line, "}")
&& line.trim().endsWith("}");
}
}

View File

@@ -0,0 +1,52 @@
package org.springframework.batch.item.file.transform;
import java.util.Map;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.MappingJsonFactory;
import org.springframework.batch.item.file.FlatFileParseException;
import org.springframework.batch.item.file.LineMapper;
/**
* Interpret a line as a Json object and parse it up to a Map. The line should
* be a standard Json object, starting with "{" and ending with "}" and composed
* of <code>name:value</code> pairs separated by commas. Whitespace is ignored,
* e.g.
*
* <pre>
* { "foo" : "bar", "value" : 123 }
* </pre>
*
* The values can also be Json objects (which are converted to maps):
*
* <pre>
* { "foo": "bar", "map": { "one": 1, "two": 2}}
* </pre>
*
* @author Dave Syer
*
*/
public class JsonLineMapper implements LineMapper<Map<String, Object>> {
private MappingJsonFactory factory = new MappingJsonFactory();
/**
* Interpret the line as a Json object and create a Map from it.
*
* @see LineMapper#mapLine(String, int)
*/
public Map<String, Object> mapLine(String line, int lineNumber) throws Exception {
Map<String, Object> result;
try {
JsonParser parser = factory.createJsonParser(line);
@SuppressWarnings("unchecked")
Map<String, Object> token = parser.readValueAs(Map.class);
result = token;
}
catch (Exception e) {
throw new FlatFileParseException("Cannot parse line to JSON", e, line, lineNumber);
}
return result;
}
}