Skip to content

Csv Serializer

Spice
import "std/text/csv-serializer";

CsvSerializer struct

Serializer for CSV (comma-separated values) output.

Fields are quoted when necessary and embedded quotes are escaped by doubling, so the output round-trips through the CsvParser from "std/text/csv-parser" when both use the same control characters.

Constructors

ctor

Spice
public p CsvSerializer.ctor(char delimiter = DEFAULT_DELIMITER, char quote = DEFAULT_QUOTE)

Construct a CSV serializer with the given control characters

Parameters

Name Type Description
delimiter char Field delimiter character (default: DEFAULT_DELIMITER)
quote char Quote character used to wrap fields (default: DEFAULT_QUOTE)

Methods

serializeField

Spice
public f<String> CsvSerializer.serializeField(const String& field)

Serializes a single field, quoting it when necessary.

A field is wrapped in quotes when it contains the delimiter, the quote character or a line terminator. Embedded quotes are escaped by doubling, so the result round-trips through parseLine/parse. The empty field is also quoted (as ""): an empty record would otherwise serialize to an empty line, which parse does not reproduce as a row.

Parameters

Name Type Description
field const String& Field value to serialize

Returns: String — Serialized (and possibly quoted) field

serializeRow

Spice
public f<String> CsvSerializer.serializeRow(const Vector<String>& row)

Serializes a single row into one CSV record. Fields are separated by the configured delimiter; no trailing line terminator is appended.

Parameters

Name Type Description
row const Vector<String>& Fields of the record

Returns: String — Serialized CSV record

serialize

Spice
public f<String> CsvSerializer.serialize(const Vector<Vector<String>>& rows)

Serializes a whole document into CSV text. Records are separated by \r\n as recommended by RFC 4180; no trailing line terminator is appended, so parse(serialize(rows)) reproduces the input rows.

Parameters

Name Type Description
rows const Vector<Vector<String>>& Rows of the document

Returns: String — Serialized CSV document