#738: KeyValuePairWriter.java

projectforge-business/src/main/java/org/projectforge/framework/utils/KeyValuePairWriter.java Lines: 157 · Author: Kai Reinhard · Type: Java — complements KeyValuePairParser 157 lines · 88 code · 53 comments · 16 blank

Purpose

Serializes key-value pairs into comma-separated text format — the reverse of KeyValuePairParser. Converts a sequence of write(key, value) calls into a single line like name="ProjectForge",version=8.0,maxUsers=100. 157 lines.

The writer handles proper quoting: string values are always wrapped in double quotes, and any " inside the value is escaped by doubling (""") — the same CSV convention that the parser expects. Numbers and dates are written without quotes. The separator character defaults to comma but is configurable via setCsvSeparator().

Overloaded write() methods — 4 variants

Separator logic

The writeSeparator() method implements the classic first-entry pattern: if this is the first entry, no separator is written; otherwise, a comma is inserted before the current entry. This avoids a trailing comma at the end of the line. The firstEntry boolean is reset with each new line (the parser's EOL triggers a new writer instance or reset).

Default separator constant

DEFAULT_SEPARATOR_CHAR = ',' is public — used by KeyValuePairParser to default its pairsSeparatorChar. Both parser and writer default to comma, and both allow changing it. The constant ensures they start with the same default.