Skip to content

Commit 557e114

Browse files
committed
1 parent aef3ae4 commit 557e114

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
@@ -8,6 +8,8 @@ Project: jackson-dataformat-csv
88

99
#93: CSV mapper does not support Views or filtering correctly for serialization
1010
(reported by triviski@github; fix contributed by Rob B)
11+
#96: SortedBy only apply to headers and actual data
12+
(reported by ShijunK@github)
1113

1214
2.6.4 (07-Dec-2015)
1315

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

+37-10
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
public class CsvSchema
6969
implements FormatSchema,
7070
Iterable<CsvSchema.Column>,
71-
java.io.Serializable // since 2.4.3
71+
java.io.Serializable // since 2.5
7272
{
7373
private static final long serialVersionUID = 1L; // 2.5
7474

@@ -261,9 +261,13 @@ public Column(int index, String name, ColumnType type, int arrayElementSep)
261261
_next = null;
262262
}
263263

264-
public Column(Column src, Column next)
264+
public Column(Column src, Column next) {
265+
this(src, src._index, next);
266+
}
267+
268+
protected Column(Column src, int index, Column next)
265269
{
266-
_index = src._index;
270+
_index = index;
267271
_name = src._name;
268272
_type = src._type;
269273
_arrayElementSeparator = src._arrayElementSeparator;
@@ -295,6 +299,16 @@ public Column withNext(Column next) {
295299
}
296300
return new Column(this, next);
297301
}
302+
303+
/**
304+
* @since 2.7
305+
*/
306+
public Column withNext(int index, Column next) {
307+
if ((_index == index) && (_next == next)) {
308+
return this;
309+
}
310+
return new Column(this, index, next);
311+
}
298312

299313
public int getIndex() { return _index; }
300314
public String getName() { return _name; }
@@ -649,7 +663,7 @@ public CsvSchema(Column[] columns, int features,
649663
_escapeChar = escapeChar;
650664
_lineSeparator = lineSeparator;
651665
_nullValue = nullValue;
652-
666+
653667
// and then we may need to create a mapping
654668
if (_columns.length == 0) {
655669
_columnsByName = Collections.emptyMap();
@@ -686,7 +700,8 @@ protected CsvSchema(Column[] columns, int features,
686700
* Copy constructor used for creating variants using
687701
* <code>sortedBy()</code> methods.
688702
*/
689-
protected CsvSchema(CsvSchema base, Column[] columns) {
703+
protected CsvSchema(CsvSchema base, Column[] columns)
704+
{
690705
_columns = _link(columns);
691706
_features = base._features;
692707
_columnSeparator = base._columnSeparator;
@@ -695,7 +710,16 @@ protected CsvSchema(CsvSchema base, Column[] columns) {
695710
_lineSeparator = base._lineSeparator;
696711
_arrayElementSeparator = base._arrayElementSeparator;
697712
_nullValue = base._nullValue;
698-
_columnsByName = base._columnsByName;
713+
714+
// and then we may need to create a mapping
715+
if (_columns.length == 0) {
716+
_columnsByName = Collections.emptyMap();
717+
} else {
718+
_columnsByName = new HashMap<String,Column>(4 + _columns.length);
719+
for (Column c : _columns) {
720+
_columnsByName.put(c.getName(), c);
721+
}
722+
}
699723
}
700724

701725
/**
@@ -716,14 +740,16 @@ protected CsvSchema(CsvSchema base, int features) {
716740
}
717741

718742
/**
719-
* Helper method used for chaining columns together using next-linkage
743+
* Helper method used for chaining columns together using next-linkage,
744+
* as well as ensuring that indexes are correct.
720745
*/
721-
private static Column[] _link(Column[] orig) {
746+
private static Column[] _link(Column[] orig)
747+
{
722748
int i = orig.length;
723749
Column[] result = new Column[i];
724750
Column prev = null;
725751
for (; --i >= 0; ) {
726-
Column curr = orig[i].withNext(prev);
752+
Column curr = orig[i].withNext(i, prev);
727753
result[i] = curr;
728754
prev = curr;
729755
}
@@ -922,7 +948,8 @@ public CsvSchema withoutColumns() {
922948
*
923949
* @since 2.4
924950
*/
925-
public CsvSchema sortedBy(String... columnNames) {
951+
public CsvSchema sortedBy(String... columnNames)
952+
{
926953
LinkedHashMap<String,Column> map = new LinkedHashMap<String,Column>();
927954
for (String colName : columnNames) {
928955
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)