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

@@ -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"));
}
}