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

MappingIterator returns List or Map depending on CSVSchema.withHeader #88

Open
flappingeagle opened this issue May 28, 2018 · 4 comments
Labels

Comments

@flappingeagle
Copy link

flappingeagle commented May 28, 2018

If CSVSchema.withHeader == true i must do:

final Map<String, String> line = (Map<String, String>) mappingIterator.nextValue();

If CSVSchema.withHeader == false i must do:

final List<String> line = (List<String>) mappingIterator.nextValue();

My use-case is: I have only one Class in which i parse CSV and this class can be configured to read the csv with header or without header (boolean-flag). For me it would be better if the MappingIterator always returns the same result-type (either a list or a map). Its not necessary, but would make things easier for me because now i need to cast the type like above, which looks a bit unsafe and is marked as yellow-warning in Eclipse-IDE.

@cowtowncoder
Copy link
Member

cowtowncoder commented May 29, 2018

I would need a full unit test (or just complete code for usage) to make sure I understand this. Behavior should not vary, regardless of whether header line exists, so I agree in that you should not have to change code there.

@flappingeagle
Copy link
Author

flappingeagle commented May 30, 2018

ok, here you go:

        // configure test --> set to true/false to test.
        final boolean withHeader = true;
        
        // prepare test-value
        final String s = "a;b;c;d;e\n1;2;3;4;5";
        final InputStream is = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));

        // lets go...
        CsvSchema schema = CsvSchema.emptySchema()
                .withColumnSeparator(';')
                .withLineSeparator("\n")
                .withQuoteChar('"');

        final ObjectReader reader;
        if (withHeader) {
            schema = schema.withHeader();
            reader = new CsvMapper().readerFor(Map.class).with(schema);
        } else {
            schema = schema.withoutHeader();
            reader = new CsvMapper().enable(CsvParser.Feature.WRAP_AS_ARRAY)
                    .readerFor(List.class).with(schema);
        }

        final MappingIterator<?> mappingIterator = reader.readValues(is);

        final Object result = mappingIterator.next();
        System.out.println(result);
        System.out.println(result.getClass()); 

You can experiment by changing the readerFor(List.class) to a readerFor(Map.class) and see that it will not work. So currently its only possible to use readerFor(List.class) for the schema.withoutHeader(). Otherwise it will not run.

@pjfanning
Copy link
Member

pjfanning commented Jun 26, 2022

@cowtowncoder the commit that closed this issue seems unrelated - I ran the OP's test case and the 'issue' still exists - there is an argument that this should still be closed as 'works as expected'

@cowtowncoder
Copy link
Member

@pjfanning Yes, I must have accidentally referred to the wrong issue on commit.

@cowtowncoder cowtowncoder reopened this Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants