@Override public void writeString(String text) throws IOException { if (text == null) { writeNull(); return; } _verifyValueWrite("write String value"); if (!_skipValue) { if (!_arraySeparator.isEmpty()) { _addToArray(text); } else { _writer.write(_columnIndex(), text); } } }
@Override protected void writeItem(CsvGenerator gen, Tuple item) throws IOException { gen.writeStartArray(); for (Object value : item.getValues()) { if (value == null) { gen.writeNull(); } else if (value instanceof Boolean) { gen.writeBoolean(((Boolean) value).booleanValue()); } else { // no point in distinguishing numbers etc; all end up as Strings anyway gen.writeString(value.toString()); } } gen.writeEndArray(); } }
protected CsvGenerator _createGenerator(IOContext ctxt, Writer out) throws IOException { CsvGenerator gen = new CsvGenerator(ctxt, _generatorFeatures, _csvGeneratorFeatures, _objectCodec, out, _schema); // any other initializations? No? return gen; }
@Override public final void writeStringField(String fieldName, String value) throws IOException, JsonGenerationException { if (_writeContext.writeFieldName(fieldName) == JsonWriteContext.STATUS_EXPECT_VALUE) { _reportError("Can not write a field name, expecting a value"); } _writeFieldName(fieldName); writeString(value); }
@Override public void writeNumber(String encodedValue) throws IOException,JsonGenerationException, UnsupportedOperationException { if (encodedValue == null) { writeNull(); return; } _verifyValueWrite("write number"); _writer.write(_columnIndex(), encodedValue); }
@Override public void writeBoolean(boolean state) throws IOException { _verifyValueWrite("write boolean value"); if (!_skipValue) { if (!_arraySeparator.isEmpty()) { _addToArray(state ? "true" : "false"); } else { _writer.write(_columnIndex(), state); } } }
@Override public void writeRawValue(String text) throws IOException { _verifyValueWrite("write Raw value"); if (!_skipValue) { // NOTE: ignore array stuff _writer.writeNonEscaped(_columnIndex(), text); } }
@Override public void writeNumber(long v) throws IOException { // First: maybe 32 bits is enough? if (v <= MAX_INT_AS_LONG && v >= MIN_INT_AS_LONG) { writeNumber((int) v); return; } _verifyValueWrite("write number"); if (!_skipValue) { if (!_arraySeparator.isEmpty()) { _addToArray(String.valueOf(v)); } else { _writer.write(_columnIndex(), v); } } }
@Override public final void writeFieldName(String name) throws IOException { if (_writeContext.writeFieldName(name) == JsonWriteContext.STATUS_EXPECT_VALUE) { _reportError("Can not write a field name, expecting a value"); } _writeFieldName(name); }
@Override public void close() throws IOException { super.close(); // Let's mark row as closed, if we had any... finishRow(); // Write the header if necessary, occurs when no rows written if (_handleFirstLine) { _handleFirstLine(); } _writer.close(_ioContext.isResourceManaged() || isEnabled(StreamWriteFeature.AUTO_CLOSE_TARGET), isEnabled(StreamWriteFeature.FLUSH_PASSED_TO_STREAM)); }
@Override public void writeOmittedField(String fieldName) throws IOException { // Hmmh. Should we require a match? Actually, let's use logic: if field found, // assumption is we must add a placeholder; if not, we can merely ignore CsvSchema.Column col = _schema.column(fieldName); if (col == null) { // assumed to have been removed from schema too } else { // basically combination of "writeFieldName()" and "writeNull()" if (_writeContext.writeFieldName(fieldName) == JsonWriteContext.STATUS_EXPECT_VALUE) { _reportError("Can not skip a field, expecting a value"); } // and all we do is just note index to use for following value write _nextColumnByName = col.getIndex(); // We can basically copy what 'writeNull()' does... _verifyValueWrite("skip positional value due to filtering"); _writer.write(_columnIndex(), ""); } }
@Override public final void writeEndArray() throws IOException { if (!_outputContext.inArray()) { _reportError("Current context not Array but "+_outputContext.typeDesc()); } _outputContext = _outputContext.getParent(); // 14-Dec-2015, tatu: To complete skipping of ignored structured value, need this: if (_skipWithin != null) { if (_outputContext == _skipWithin) { _skipWithin = null; } return; } if (!_arraySeparator.isEmpty()) { _arraySeparator = CsvSchema.NO_ARRAY_ELEMENT_SEPARATOR; _writer.write(_columnIndex(), _arrayContents.toString()); } // 20-Nov-2014, tatu: When doing "untyped"/"raw" output, this means that row // is now done. But not if writing such an array field, so: if (!_outputContext.inObject()) { finishRow(); } }
@Override public final void writeStartArray() throws IOException _verifyValueWrite("start an array"); && _skipValue && isEnabled(StreamWriteFeature.IGNORE_UNKNOWN)) { _skipWithin = _outputContext; } else if (!_skipValue) { _reportError("CSV generator does not support Array values for properties without setting 'arrayElementSeparator' in schema"); if (!_arraySeparator.isEmpty()) { _reportError("CSV generator does not support nested Array values");
@Override public void writeNumber(long l) throws IOException, JsonGenerationException { // First: maybe 32 bits is enough? if (l <= MAX_INT_AS_LONG && l >= MIN_INT_AS_LONG) { writeNumber((int) l); return; } _verifyValueWrite("write number"); _writer.write(_columnIndex(), l); }
@Override public final void writeEndArray() throws IOException, JsonGenerationException { if (!_writeContext.inArray()) { _reportError("Current context not an ARRAY but "+_writeContext.getTypeDesc()); } _writeContext = _writeContext.getParent(); // not 100% fool-proof, but chances are row should be done now finishRow(); }
@Override public final void writeStartObject() throws IOException { _verifyValueWrite("start an object"); // No nesting for objects; can write Objects inside logical root-level arrays. // 14-Dec-2015, tatu: ... except, should be fine if we are ignoring the property if (_writeContext.inObject() || // 07-Nov-2017, tatu: But we may actually be nested indirectly; so check (_writeContext.inArray() && !_writeContext.getParent().inRoot())) { if (_skipWithin == null) { // new in 2.7 if (_skipValue && isEnabled(JsonGenerator.Feature.IGNORE_UNKNOWN)) { _skipWithin = _writeContext; } else { _reportMappingError("CSV generator does not support Object values for properties (nested Objects)"); } } } _writeContext = _writeContext.createChildObjectContext(); }
@Override protected final void _verifyValueWrite(String typeMsg) throws IOException, JsonGenerationException { int status = _writeContext.writeValue(); if (status == JsonWriteContext.STATUS_EXPECT_NAME) { _reportError("Can not "+typeMsg+", expecting field name"); } if (_handleFirstLine) { _handleFirstLine(); } }
@Override public void writeRawUTF8String(byte[] text, int offset, int len) throws IOException, JsonGenerationException { _reportUnsupportedOperation(); }
@Override public final void writeStartObject() throws IOException, JsonGenerationException { _verifyValueWrite("start an object"); /* No nesting for objects; can write Objects inside logical * root-level arrays. */ if (_writeContext.inObject()) { _reportError("CSV generator does not support Object values for properties"); } _writeContext = _writeContext.createChildObjectContext(); }