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

SequenceWriter#write(null) writes a single null, not an entire row of nulls #69

Closed
georgewfraser opened this issue Feb 9, 2015 · 4 comments
Milestone

Comments

@georgewfraser
Copy link
Contributor

It requires very specific conditions, it is reproduced here (along with a bunch of similar cases that DO work):

https://gist.github.com/georgewfraser/ef19edb1eca296bd3234

It would be OK to just skip writing the row, but this behavior is really bad because if you write a lot nulls you end up with a long row with too many columns like ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

@georgewfraser
Copy link
Contributor Author

Possible fix to CsvGenerator.java:

    public void writeNull() throws IOException {
        _verifyValueWrite("write null value");
        if (!_skipValue) {
            if (_arraySeparator >= 0) {
                _addToArray(_schema.getNullValue());
            } else {
                _writer.writeNull(_columnIndex());
            }

            // If we are told to write null, but we were never told to begin an object
            // that means the entire row is just a null, so we are now finished writing the row
            if (!_writeContext.inObject()) {
                finishRow();
            }
        }
    }

The last part just guesses that we're in the row = null case if we're writing a null and not inside an object. I'm not sure whether it belongs inside or outside if(!_skipValue) because I don't know what that is about.

@cowtowncoder
Copy link
Member

I agree, exact semantics are bit tricky. Writing an empty row sounds like a reasonable way to go, although this may cause problems when using custom null representation. But we can cross that bridge when we get there.

@georgewfraser
Copy link
Contributor Author

Until a fix makes it into the next Jackson release, here's a workaround for anyone who needs it. Usage is new CsvMapper(new PatchCsvFactory())

https://gist.github.com/georgewfraser/52780f7d82d2b5ec80dc

@cowtowncoder
Copy link
Member

Interestingly it is quite easy to add one of two possibilities:

  1. Simply output nothing (ignore)
  2. Output "empty Object" (all commas)

so it'd even be possible to configure behavior.

I'll go ahead and implement (1) first.

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