Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit e0e8bd7

Browse files
committed
Merge branch '2.6'
2 parents ca2e81b + 557e114 commit e0e8bd7

File tree

3 files changed

+76
-10
lines changed

3 files changed

+76
-10
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project: jackson-dataformat-csv
2121

2222
#93: CSV mapper does not support Views or filtering correctly for serialization
2323
(reported by triviski@github; fix contributed by Rob B)
24+
#96: SortedBy only apply to headers and actual data
25+
(reported by ShijunK@github)
2426

2527
2.6.4 (07-Dec-2015)
2628

src/main/java/com/fasterxml/jackson/dataformat/csv/CsvSchema.java

+37-10
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
public class CsvSchema
7878
implements FormatSchema,
7979
Iterable<CsvSchema.Column>,
80-
java.io.Serializable // since 2.4.3
80+
java.io.Serializable // since 2.5
8181
{
8282
private static final long serialVersionUID = 1L; // 2.5
8383

@@ -289,9 +289,13 @@ public Column(int index, String name, ColumnType type, String arrayElementSep)
289289
_next = null;
290290
}
291291

292-
public Column(Column src, Column next)
292+
public Column(Column src, Column next) {
293+
this(src, src._index, next);
294+
}
295+
296+
protected Column(Column src, int index, Column next)
293297
{
294-
_index = src._index;
298+
_index = index;
295299
_name = src._name;
296300
_type = src._type;
297301
_arrayElementSeparator = src._arrayElementSeparator;
@@ -334,6 +338,16 @@ public Column withNext(Column next) {
334338
}
335339
return new Column(this, next);
336340
}
341+
342+
/**
343+
* @since 2.7
344+
*/
345+
public Column withNext(int index, Column next) {
346+
if ((_index == index) && (_next == next)) {
347+
return this;
348+
}
349+
return new Column(this, index, next);
350+
}
337351

338352
public int getIndex() { return _index; }
339353
public String getName() { return _name; }
@@ -768,7 +782,7 @@ public CsvSchema(Column[] columns, int features,
768782
_escapeChar = escapeChar;
769783
_lineSeparator = lineSeparator;
770784
_nullValue = nullValue;
771-
785+
772786
// and then we may need to create a mapping
773787
if (_columns.length == 0) {
774788
_columnsByName = Collections.emptyMap();
@@ -805,7 +819,8 @@ protected CsvSchema(Column[] columns, int features,
805819
* Copy constructor used for creating variants using
806820
* <code>sortedBy()</code> methods.
807821
*/
808-
protected CsvSchema(CsvSchema base, Column[] columns) {
822+
protected CsvSchema(CsvSchema base, Column[] columns)
823+
{
809824
_columns = _link(columns);
810825
_features = base._features;
811826
_columnSeparator = base._columnSeparator;
@@ -814,7 +829,16 @@ protected CsvSchema(CsvSchema base, Column[] columns) {
814829
_lineSeparator = base._lineSeparator;
815830
_arrayElementSeparator = base._arrayElementSeparator;
816831
_nullValue = base._nullValue;
817-
_columnsByName = base._columnsByName;
832+
833+
// and then we may need to create a mapping
834+
if (_columns.length == 0) {
835+
_columnsByName = Collections.emptyMap();
836+
} else {
837+
_columnsByName = new HashMap<String,Column>(4 + _columns.length);
838+
for (Column c : _columns) {
839+
_columnsByName.put(c.getName(), c);
840+
}
841+
}
818842
}
819843

820844
/**
@@ -835,14 +859,16 @@ protected CsvSchema(CsvSchema base, int features) {
835859
}
836860

837861
/**
838-
* Helper method used for chaining columns together using next-linkage
862+
* Helper method used for chaining columns together using next-linkage,
863+
* as well as ensuring that indexes are correct.
839864
*/
840-
private static Column[] _link(Column[] orig) {
865+
private static Column[] _link(Column[] orig)
866+
{
841867
int i = orig.length;
842868
Column[] result = new Column[i];
843869
Column prev = null;
844870
for (; --i >= 0; ) {
845-
Column curr = orig[i].withNext(prev);
871+
Column curr = orig[i].withNext(i, prev);
846872
result[i] = curr;
847873
prev = curr;
848874
}
@@ -1080,7 +1106,8 @@ public CsvSchema withoutColumns() {
10801106
*
10811107
* @since 2.4
10821108
*/
1083-
public CsvSchema sortedBy(String... columnNames) {
1109+
public CsvSchema sortedBy(String... columnNames)
1110+
{
10841111
LinkedHashMap<String,Column> map = new LinkedHashMap<String,Column>();
10851112
for (String colName : columnNames) {
10861113
Column col = _columnsByName.get(colName);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fasterxml.jackson.dataformat.csv.ser;
2+
3+
import java.util.Arrays;
4+
5+
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
6+
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
7+
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
8+
9+
public class SchemaReorderTest extends ModuleTestBase
10+
{
11+
// should work ok since CsvMapper forces alphabetic ordering as default:
12+
static class Reordered {
13+
public int a, b, c, d;
14+
}
15+
16+
private final CsvMapper MAPPER = new CsvMapper();
17+
18+
public void testSchemaWithOrdering() throws Exception
19+
{
20+
CsvSchema schema = MAPPER.schemaFor(Reordered.class);
21+
assertEquals(aposToQuotes("['a','b','c','d']"), schema.getColumnDesc());
22+
schema = schema.sortedBy("b", "c");
23+
assertEquals(aposToQuotes("['b','c','a','d']"), schema.getColumnDesc());
24+
25+
Reordered value = new Reordered();
26+
value.a = 1;
27+
value.b = 2;
28+
value.c = 3;
29+
value.d = 4;
30+
31+
schema = schema.withHeader();
32+
String csv = MAPPER.writer(schema).writeValueAsString(Arrays.asList(value));
33+
assertEquals("b,c,a,d\n2,3,1,4\n", csv);
34+
35+
// _verifyLinks(schema);
36+
}
37+
}

0 commit comments

Comments
 (0)