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

CsvMapper and @JsonFilter annotation #20

Closed
mablaev opened this issue Aug 9, 2013 · 3 comments
Closed

CsvMapper and @JsonFilter annotation #20

mablaev opened this issue Aug 9, 2013 · 3 comments

Comments

@mablaev
Copy link

mablaev commented Aug 9, 2013

CsvMapper does not generate valid output, when I trying to use it against bean with @jsonfilter annotation.
There are 2 problems here:

  1. I use csv schema with header generation. Filter do not affect on header.
  2. JsonFilter damages csv content by removing coma column separator.

public class AppTest {

@Test
public void testCsvMapperWithJsonFilter() throws JsonProcessingException {
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(Company.class).withLineSeparator("\n").withHeader();

    SimpleFilterProvider filterProvider = new SimpleFilterProvider()
            .addFilter(COMPANY_FILTER, FilterExceptFilter.filterOutAllExcept("name", "ticker"));

    String actual = mapper.writer(filterProvider).withSchema(schema).writeValueAsString(createCompanies());
    System.out.println(actual);

    StringBuilder expected = new StringBuilder("name,ticker")
            .append('\n')
            .append("name1,ticker1")
            .append('\n')
            .append("name2,ticker2")
            .append('\n')
            .append("name3,ticker3");

    Assert.assertEquals(expected, actual);
}

private static List<Company> createCompanies() {
    return Arrays.asList(
            new Company(1, "name1", "ticker1")
            , new Company(2, "name2", "ticker2")
            , new Company(3, "name3", "ticker3"));
}

@JsonFilter(COMPANY_FILTER)
public static class Company {
    static final String COMPANY_FILTER = "COMPANY_FILTER";

    private int id;
    private String name;
    private String ticker;

    private Company(int id, String name, String ticker) {
        this.id = id;
        this.name = name;
        this.ticker = ticker;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTicker() {
        return ticker;
    }

    public void setTicker(String ticker) {
        this.ticker = ticker;
    }
}

}

@cowtowncoder
Copy link
Member

I will try to solve this as far as possible.

One part that may be difficult is that of doing anything to header line: I realize that ideally header columns would be removed. This may be difficult to handle mostly because data-binder has no knowledge of special handling of data formats.

But even if headers could not be filtered, it should be possible to add placeholders (empty values) for missing data.

cowtowncoder added a commit that referenced this issue Aug 11, 2013
@cowtowncoder
Copy link
Member

Ok: I implemented at least partial support for both @JsonView and @JsonFilter-based filtering. In both cases, values of properties indicated to be filtered out are omitted and empty value is included instead. Header line is left as-is, for this version as I could not yet figure out how to exclude it.

What I am thinking is that this baseline support will allow some level of filtering; and the additional (possibly optional) feature of excluding column name and value completely from output can and should be filed as a separate feature request. This is mostly so that release notes will include exact information of what is included and works.

@mablaev
Copy link
Author

mablaev commented Aug 12, 2013

thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants