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

Deserializing an empty string as an array field return a non-empty list of one empty String #66

Closed
aksh3ll opened this issue Jan 28, 2015 · 1 comment
Milestone

Comments

@aksh3ll
Copy link

aksh3ll commented Jan 28, 2015

I have a csv with 11 columns separated with tabulation the last one is an array separated by pipes.
I've declared a schema for the maping.
When the array field have one or many values it works fine, but when it has no value, instead of an empty list, i get a list of one empty String "".

I've found this thread http://stackoverflow.com/questions/12933394/jackson-deserialize-as-an-empty-list but, even with adding the DeserializationFeature presented it didn't change anything (i've try with and withFeatures, i don't see the difference?).

Even if i can't modify the source file, i've tried with quotes, it doesn't change anything.

        CsvSchema schema = CsvSchema.builder()
                .setColumnSeparator('\t')
                .setArrayElementSeparator('|')
                .disableEscapeChar()
                .disableQuoteChar()
                .setUseHeader(false)
                .setNullValue("")
                .addNumberColumn("extId") // 0
                .addNumberColumn("intId") // 1
                .addNumberColumn("pct") // 2
                .addNumberColumn("mask") // 3
                .addColumn(Column.PLACEHOLDER) // 4
                .addColumn("singName") // 5
                .addColumn("PlurName") // 6
                .addColumn(Column.PLACEHOLDER) // 7
                .addColumn(Column.PLACEHOLDER) // 8
                .addColumn(Column.PLACEHOLDER) // 9
                .addArrayColumn("aliases") // 10
                .build();

        CsvMapper mapper = new CsvMapper();
        this.it = mapper
                .reader(clazz)
                .withFeatures(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, 
                        DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT,
                        DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)
                .with(schema)
                .readValues(reader);

According to the idea of the thread i've modified my setter, but it's not a good solution.

    @JsonProperty("aliases")
    public void setAliases(List<String> aliases) {
        if (aliases == null || (aliases.size() == 1 && "".equals(aliases.get(0))))
            this.aliases = new ArrayList<String>();
        else
            this.aliases = aliases;
    }

Maybe i've missed something?

@cowtowncoder
Copy link
Member

This sounds more like a bug; and since array handling was added in 2.5.0, I suspect it is just an edge case that was not tested or handled correctly.
Thank you for reproting this!

@cowtowncoder cowtowncoder modified the milestones: 2.3.0, 2.5.2 Feb 12, 2015
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