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

CSV string double quotes: String length is also considered? #280

Open
blackr1234 opened this issue Jul 20, 2021 · 1 comment
Open

CSV string double quotes: String length is also considered? #280

blackr1234 opened this issue Jul 20, 2021 · 1 comment

Comments

@blackr1234
Copy link

I'd like to know why jackson-dataformat-csv:

  • Does not quote alphanumeric strings with length <25 (<=24)
  • Quotes alphanumeric strings with length >=25

Probably it is documented somewhere. If anyone knows, please let me know.

Thanks.


Sample code

public class Main {

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

        final List<Foo> list = Arrays.asList(
            new Foo("a23456789012345678901234"),
            new Foo("a234567890123456789012345")
        );

        final CsvMapper mapper = new CsvMapper();

        final CsvSchema.Builder schemaBuilder = CsvSchema.builder().setUseHeader(true);
        schemaBuilder.addColumn("name");
        final CsvSchema schema = schemaBuilder.build();

        final ObjectWriter writer = mapper.writerFor(Foo.class).with(schema);
        writer.writeValues(System.out).writeAll(list);

        // name
        // a23456789012345678901234
        // "a234567890123456789012345"
    }
}

class Foo {
    private String name;

    public Foo(String name) {
        setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
@cowtowncoder
Copy link
Member

Short answer is that CsvEncoder, by default, uses fast heuristics to determine if quoting is or may be needed: 24 happens to be the limit defined as CsvEncoder.MAX_QUOTE_CHECK. I am not sure this is documented in README; PRs are welcome for changes to improve explanations.

There are settings to change this behavior:

  • CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING (default: false) -- if enabled, check will be performed using full String value, no matter the length
  • CsvGenerator.Feature.ALWAYS_QUOTE_STRINGS (default: false) -- if enabled, no checks are performance and quoting is always applied.

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

No branches or pull requests

2 participants