Bodu.Text.Delimited.Writer Namespace
- Package
-
Bodu.Text.Delimited 1.3.0
Purpose
Bodu.Text.Delimited.Writer is the emitting counterpart of Bodu.Text.Delimited.Reader in Bodu.Text.Delimited: a forward-only writer that produces RFC 4180 delimited text as UTF-8 into an IBufferWriter<byte> or a Stream. The DelimitedSerializer and the mutable DelimitedNode DOM (WriteTo) emit through it.
Key types
- Utf8DelimitedWriter - the
ref structwriter:WriteStartArray/WriteEndArrayaround the record set,WriteStartObject/WritePropertyName/WriteString/WriteEndObjectfor header-keyed records (or a nested array ofWriteStringfor positional rows), plusFlush,Dispose,BytesCommitted, andBytesPending. - DelimitedWriterOptions -
Delimiter,Quote,CommentChar, andNoHeader.
Example
using System.Buffers;
using Bodu.Text.Delimited.Writer;
var buffer = new ArrayBufferWriter<byte>();
var writer = new Utf8DelimitedWriter(buffer, new DelimitedWriterOptions { Delimiter = '\t' });
writer.WriteStartArray();
writer.WriteStartObject();
writer.WritePropertyName("symbol");
writer.WriteString("AAPL");
writer.WritePropertyName("qty");
writer.WriteString("10");
writer.WriteEndObject();
writer.WriteEndArray();
writer.Flush(); // "symbol\tqty\r\nAAPL\t10\r\n"
Notes
- Header from the first record. The property names of the first object record become the header row; subsequent records contribute value rows only.
- Minimal quoting. A field is quoted, with internal quotes doubled, only when the reader would otherwise read it back differently: when it contains the delimiter, the quote character, or a line break; when it begins or ends with a space or a tab; when it is a record's first field and begins with
CommentChar; when it is the first field the writer writes and begins with U+FEFF, which the reader would skip as a byte order mark; or when it is the only field of its record and is empty. Output therefore round-trips through the reader. - CRLF line endings. Every row, the header row included, ends with a carriage return and a line feed, as RFC 4180 requires, whatever the platform.
- Flush to commit in stream mode. Over a
Stream, bytes are staged untilFlush(orDispose), andBytesPendingshows what is still buffered; over anIBufferWriter<byte>, as in the example, they go straight to the buffer andFlushdoes nothing. - See also: the line-formats introduction and the delimited guide.
Structs
- DelimitedWriterOptions
Provides configuration for a Utf8DelimitedWriter, controlling the delimiter, quote character, the comment character the writer guards, and whether a header row is emitted for object records.
- Utf8DelimitedWriter
Provides a forward-only writer that emits delimited (RFC 4180 CSV/TSV) text as UTF-8 bytes to an IBufferWriter<T> or a Stream. The writer is a
ref struct.