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

Improve support of embedded CSV #212

Open
charphi opened this issue Mar 12, 2024 · 1 comment
Open

Improve support of embedded CSV #212

charphi opened this issue Mar 12, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@charphi
Copy link
Member

charphi commented Mar 12, 2024

The current implementation is inefficient because the internal buffer is recreated for each call.
There should be a way to reuse the buffer or the reader/writer between calls.

There is also a problem with the last end-of-line which is not welcome in some cases.

Embedded CSV example:

public class EmbeddedCSVDemo {

    public static void main(String[] args) throws IOException {

        Csv.Format embeddedFormat = Csv.Format.builder().delimiter('=').separator(",").build();

        StringWriter mainString = new StringWriter();
        try (Csv.Writer main = Csv.Writer.of(Csv.Format.DEFAULT, Csv.WriterOptions.DEFAULT, mainString)) {
            main.writeField("NAME");
            main.writeField("PROPERTIES");
            main.writeEndOfLine();

            for (Record record : Record.getData()) {
                main.writeField(record.getName());
                StringWriter embeddedString = new StringWriter();
                try (Csv.Writer embedded = Csv.Writer.of(embeddedFormat, Csv.WriterOptions.DEFAULT, embeddedString)) {
                    for (Map.Entry<String, String> property : record.getProperties().entrySet()) {
                        embedded.writeField(property.getKey());
                        embedded.writeField(property.getValue());
                        embedded.writeEndOfLine();
                    }
                }
                main.writeField(embeddedString.toString());
                main.writeEndOfLine();
            }
        }
        System.out.println(mainString);
    }

    @lombok.Value
    @lombok.Builder
    public static class Record {

        String name;

        @lombok.Singular
        Map<String, String> properties;

        static List<Record> getData() {
            List<Record> result = new ArrayList<>();
            result.add(Record.builder().name("name1").property("key1", "value1").build());
            result.add(Record.builder().name("name2").property("key2", "value2").build());
            return result;
        }
    }
}

Output:

NAME,PROPERTIES
name1,"key1=value1,"
name2,"key2=value2,"
@charphi charphi added the enhancement New feature or request label Mar 12, 2024
@charphi
Copy link
Member Author

charphi commented Mar 12, 2024

Related work: osiegmar/FastCSV#63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant