|
2 | 2 |
|
3 | 3 | import java.io.ByteArrayOutputStream;
|
4 | 4 | import java.io.StringWriter;
|
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
5 | 7 |
|
6 | 8 | import com.fasterxml.jackson.databind.*;
|
7 | 9 | import com.fasterxml.jackson.dataformat.csv.*;
|
8 | 10 |
|
9 | 11 | // for [dataformat-csv#69], other null value serialization
|
10 | 12 | public class NullWritingTest extends ModuleTestBase
|
11 | 13 | {
|
12 |
| - private final CsvMapper csv = new CsvMapper(); |
13 |
| - |
14 | 14 | public static class Nullable {
|
15 | 15 | public String a, b, c, d;
|
16 | 16 | }
|
17 |
| - |
| 17 | + |
| 18 | + private final CsvMapper csv = new CsvMapper(); |
| 19 | + |
18 | 20 | public void testObjectWithNullMembersToString() throws Exception {
|
19 | 21 | CsvSchema schema = csv.schemaFor(Nullable.class).withUseHeader(true);
|
20 | 22 | ObjectWriter writer = csv.writer(schema);
|
@@ -78,4 +80,44 @@ public void testCustomNullValue() throws Exception
|
78 | 80 | // MUST use doubling for quotes!
|
79 | 81 | assertEquals("id,n/a\n", result);
|
80 | 82 | }
|
| 83 | + |
| 84 | + public void testNullFieldsOfListsContainedByMainLevelListIssue106() throws Exception |
| 85 | + { |
| 86 | + CsvMapper mapper = mapperForCsv(); |
| 87 | + CsvSchema schema = CsvSchema.builder().build(); |
| 88 | + |
| 89 | + List<String> row1 = Arrays.asList("d0", null, "d2"); |
| 90 | + List<String> row2 = Arrays.asList(null, "d1", "d2"); |
| 91 | + List<String> row3 = Arrays.asList("d0", "d1", null); |
| 92 | + |
| 93 | + List<List<String>> dataList = Arrays.asList(row1, row2, row3); |
| 94 | + |
| 95 | + String result = mapper.writer(schema).writeValueAsString(dataList); |
| 96 | + assertEquals("d0,,d2\n,d1,d2\nd0,d1,\n", result); |
| 97 | + |
| 98 | + schema = schema.withNullValue("n/a"); |
| 99 | + result = mapper.writer(schema).writeValueAsString(dataList); |
| 100 | + assertEquals("d0,n/a,d2\nn/a,d1,d2\nd0,d1,n/a\n", result); |
| 101 | + } |
| 102 | + |
| 103 | + public void testNullElementsOfMainLevelListIssue106() throws Exception |
| 104 | + { |
| 105 | + CsvMapper mapper = mapperForCsv(); |
| 106 | + CsvSchema schema = CsvSchema.builder().build(); |
| 107 | + |
| 108 | + List<String> row1 = Arrays.asList("d0", null, "d2"); |
| 109 | + List<String> row2 = Arrays.asList(null, "d1", "d2"); |
| 110 | + List<String> row3 = Arrays.asList("d0", "d1", null); |
| 111 | + |
| 112 | + // when serialized, the added root level nulls at index 1 and 3 |
| 113 | + // should be absent from the output |
| 114 | + List<List<String>> dataList = Arrays.asList(row1, null, row2, null, row3); |
| 115 | + |
| 116 | + String result = mapper.writer(schema).writeValueAsString(dataList); |
| 117 | + assertEquals("d0,,d2\n,d1,d2\nd0,d1,\n", result); |
| 118 | + |
| 119 | + schema = schema.withNullValue("n/a"); |
| 120 | + result = mapper.writer(schema).writeValueAsString(dataList); |
| 121 | + assertEquals("d0,n/a,d2\nn/a,d1,d2\nd0,d1,n/a\n", result); |
| 122 | + } |
81 | 123 | }
|
0 commit comments