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

Allow skipping ending line break (CsvGenerator.Feature.WRITE_LINEFEED_AFTER_LAST_ROW) #45

Closed
mathieu-lavigne opened this issue Oct 23, 2017 · 11 comments
Labels
2.17 Fix or feature targeted at 2.17 release csv
Milestone

Comments

@mathieu-lavigne
Copy link

In RFC-4180 it is said that CSV implementations may choose to include a line break at the end of the file or not.

It could be nice to add an option to choose. For instance something like this :

schema.withEndingLineSeparator();
schema.withoutEndingLineSeparator();
mathieu-lavigne added a commit to mathieu-lavigne/jackson-dataformats-text that referenced this issue Oct 23, 2017
@cowtowncoder
Copy link
Member

Ok, sounds reasonable from usage perspective. Not sure how easy it'd be to do, wrt. when determination is made regarding adding a linefeed: if it's at the time of starting a new line, this should be quite easy.

There is also the question of whether this belongs in schema, or as a CsvGenerator.Feature. I can see either being possible choice.

@tyler2cr
Copy link

tyler2cr commented Aug 20, 2019

2.9.9 does not include #46

should the Labels or Milestone be changed?

@tyler2cr
Copy link

This requested feature may resolve an unexpected scenario:

These tests fail with java.lang.AssertionError: expected null, but was:<>

I've been unable to determine where the <> value comes from.

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class newlinetest {

    @JsonPropertyOrder({"column1", "column2"})
    static
    class Pojo {
        private String column1;
        private String column2;

        public String getColumn1() {
            return column1;
        }

        public void setColumn1(String column1) {
            this.column1 = column1;
        }

        public String getColumn2() {
            return column2;
        }

        public void setColumn2(String column2) {
            this.column2 = column2;
        }
    }

    @Test
    public void two_rows() throws IOException {
        // setup
        String csv = "value|\nvalue|\n";
        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema = csvMapper.schemaFor(Pojo.class).withColumnSeparator('|');

        // execute
        MappingIterator<Pojo> mappingIterator = csvMapper.readerFor(Pojo.class).with(csvSchema).readValues(csv);

        // test
        assertTrue(mappingIterator.hasNext());
        Pojo row1 = mappingIterator.next();
        assertNotNull(row1);
        assertEquals("value", row1.column1);
        assertNull(row1.column2);

    }

    @Test
    public void one_row() throws IOException {
        // setup
        String csv = "value|\n";
        CsvMapper csvMapper = new CsvMapper();
        CsvSchema csvSchema = csvMapper.schemaFor(Pojo.class).withColumnSeparator('|');

        // execute
        Pojo pojo = csvMapper.readerFor(Pojo.class).with(csvSchema).readValue(csv);

        // test
        assertNotNull(pojo);
        assertEquals("value", pojo.column1);
        assertNull(pojo.column2);
    }
}

@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Aug 20, 2019
@cowtowncoder
Copy link
Member

@tyler2cr Label is purely aspirational, i.e. at the time of update possible target. Updated to 2.10 as this can not be added in 2.9 any more; 2.10 is still possible. Milestone will indicate actual inclusion if and when that happens.

@rage-shadowman
Copy link

@tyler2cr the "<>" is just how JUnit describes an empty string in the assertNull error message.

@tyler2cr
Copy link

@cowtowncoder Sounds good - thanks for the quick response!

@tyler2cr
Copy link

@rage-shadowman 🤦‍♂ thank you!!

After this realization, I used .withNullValue("") on the CsvSchema, and it works like a charm

@cowtowncoder cowtowncoder removed the 2.10 label Nov 10, 2020
@alturkovic
Copy link

Any progress on this?

@cowtowncoder
Copy link
Member

cowtowncoder commented Dec 22, 2023

@alturkovic I don't think so. I definitely haven't had time to try to implement this.

But if (or anyone else) has time and interest, would be happy to help get a contributed PR merged.

EDIT: I forgot there's a PR -- #46 - that proposes a way to do it. Unfortunately I don't think it quite works reliably in that there might be a case where multi-character linefeed (\r\n f.ex) could be split on buffer boundary.
But maybe it'd be possible to rework this.

On changes to #46: I think that this should be enabled using CsvGenerator.Feature instead of CsvSchema.
That would simplify PR a bit too.

Aside from that I think ensuring "undo last linefeed" works reliably is probably doable.

@cowtowncoder
Copy link
Member

Hmmh. Actually, code for CsvEncoder.endRow() already ensures linefeed is not split.
So code might just work.

I'll add a note on #46 wrt other change.

@cowtowncoder cowtowncoder changed the title Option to skip ending line break Option to skip ending line break (CsvGenerator.Feature.WRITE_LINEFEED_AFTER_LAST_ROW) Jan 15, 2024
@cowtowncoder cowtowncoder changed the title Option to skip ending line break (CsvGenerator.Feature.WRITE_LINEFEED_AFTER_LAST_ROW) Allow skipping ending line break (CsvGenerator.Feature.WRITE_LINEFEED_AFTER_LAST_ROW) Jan 15, 2024
@cowtowncoder cowtowncoder added this to the 2.17.0 milestone Jan 15, 2024
@cowtowncoder cowtowncoder added the 2.17 Fix or feature targeted at 2.17 release label Jan 15, 2024
@cowtowncoder
Copy link
Member

Implemented for 2.17.0 (see #453)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.17 Fix or feature targeted at 2.17 release csv
Projects
None yet
Development

No branches or pull requests

5 participants