Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CsvMapper now honors JsonProperty index. #159

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.fasterxml.jackson.dataformat.csv;

import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsLast;

import java.util.Collection;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;

Expand Down Expand Up @@ -459,7 +464,13 @@ protected void _addSchemaProperties(SerializerProvider ctxt, CsvSchema.Builder b
}
BeanDescription beanDesc = ctxt.introspectBeanDescription(pojoType);
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
for (BeanPropertyDefinition prop : beanDesc.findProperties()) {
List<BeanPropertyDefinition> properties = beanDesc.findProperties();

// soring properties by @JsonProperty index
properties.sort(comparing(o -> intr.findPropertyIndex(ctxt.getConfig(), o.getPrimaryMember()),
Copy link
Member

@cowtowncoder cowtowncoder Nov 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jackson 2.x requires JDK 7, can not yet use JDK 8 features (but easy enough to change)

nullsLast(naturalOrder())));

for (BeanPropertyDefinition prop : properties) {
// ignore setter-only properties:
if (!prop.couldSerialize()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
Expand Down Expand Up @@ -41,7 +42,17 @@ static abstract class YZ {
public abstract int getY();
public abstract int getZ();
}


private static class PointWithExplicitIndices {
@JsonProperty(required = true, value = "y", index = 1)
public int y;

@JsonProperty(required = true, value = "x", index = 2)
public int x;

public int z;
}

/*
/**********************************************************************
/* Test methods
Expand Down Expand Up @@ -206,4 +217,16 @@ public void testSchemaComposition() throws Exception
assertEquals("lastName", it.next().getName());
assertEquals("x", it.next().getName());
}

// For [dataformat-csv#115]: honor JsonProperty index
public void testSchemaWithExplicitIndices()
{
CsvSchema pointSchema = MAPPER.typedSchemaFor(PointWithExplicitIndices.class);

assertEquals("y", pointSchema.column(0).getName());
assertEquals("x", pointSchema.column(1).getName());
assertEquals("z", pointSchema.column(2).getName());

_verifyLinks(pointSchema);
}
}