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

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-batch-infrastructure</artifactId>
<version>2.1.0.CI-SNAPSHOT </version>
@@ -100,6 +101,12 @@
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.0.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>

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;
}
}

View File

@@ -0,0 +1,23 @@
package org.springframework.batch.item.file.separator;
import static org.junit.Assert.*;
import org.junit.Test;
public class JsonRecordSeparatorPolicyTests {
private JsonRecordSeparatorPolicy policy = new JsonRecordSeparatorPolicy();
@Test
public void testIsEndOfRecord() {
assertFalse(policy.isEndOfRecord("{\"a\":\"b\""));
assertTrue(policy.isEndOfRecord("{\"a\":\"b\"} "));
}
@Test
public void testNestedObject() {
assertFalse(policy.isEndOfRecord("{\"a\": {\"b\": 2}"));
assertTrue(policy.isEndOfRecord("{\"a\": {\"b\": 2}} "));
}
}

View File

@@ -0,0 +1,34 @@
package org.springframework.batch.item.file.transform;
import static org.junit.Assert.*;
import java.util.Map;
import org.junit.Test;
import org.springframework.batch.item.file.FlatFileParseException;
public class JsonLineMapperTests {
private JsonLineMapper mapper = new JsonLineMapper();
@Test
public void testMapLine() throws Exception {
Map<String, Object> map = mapper.mapLine("{\"foo\": 1}", 1);
assertEquals(1, map.get("foo"));
}
@SuppressWarnings("unchecked")
@Test
public void testMapNested() throws Exception {
Map<String, Object> map = mapper.mapLine("{\"foo\": 1, \"bar\" : {\"foo\": 2}}", 1);
assertEquals(1, map.get("foo"));
assertEquals(2, ((Map<String, Object>) map.get("bar")).get("foo"));
}
@Test(expected=FlatFileParseException.class)
public void testMappingError() throws Exception {
Map<String, Object> map = mapper.mapLine("{\"foo\": 1", 1);
assertEquals(1, map.get("foo"));
}
}