Chapter 34 Key Takeaways

Format Selection Guide

Format Use When Free Pascal Unit
INI Simple key-value configuration IniFiles (TINIFile)
JSON Structured data exchange, APIs, nested config fpjson, jsonparser
XML Document-oriented data, enterprise integration DOM, XMLRead, XMLWrite
CSV Tabular data, spreadsheet import/export Custom parser (no built-in)
Binary Performance-critical internal storage BlockRead/BlockWrite, packed record

INI Files

  • TINIFile in the IniFiles unit handles all INI operations
  • Every Read method takes a default value — the app works even without a config file
  • Call UpdateFile to flush writes to disk
  • INI is flat: sections + key-value pairs, no nesting

JSON (fpjson)

  • GetJSON(string) parses a JSON string into TJSONData
  • TJSONObject.Add(key, value) builds objects; TJSONArray.Add(value) builds arrays
  • FormatJSON = pretty-printed; AsJSON = compact
  • Memory rule: Parent owns children. Free only the root object.
  • JSON has no date type — use ISO 8601 strings (yyyy-mm-dd)

XML (DOM)

  • ReadXMLFile(Doc, filename) parses XML; WriteXMLFile(Doc, filename) writes it
  • Navigate with DocumentElement, FindNode, FirstChild, NextSibling
  • Attributes: TDOMElement.GetAttribute / SetAttribute
  • Text content: Node.TextContent

CSV

  • Writing: quote fields that contain commas, quotes, or newlines
  • Escaped quotes: double them ("" inside a quoted field)
  • Reading: use a state machine with an InQuote flag
  • Bank CSVs vary wildly — make delimiters, date formats, and column mappings configurable

Binary Formats

  • Use packed record to eliminate compiler alignment padding
  • Include a magic number and version field in the header
  • Support version-based branching in the reader
  • Binary is 10-30x faster than text formats for large datasets

Serialization Patterns

  • Define a common interface (IExpenseSerializer) implemented by format-specific classes
  • Map record fields to format elements (JSON keys, XML elements, CSV columns)
  • Handle missing fields with defaults, extra fields with tolerance
  • Verify round-trip integrity: deserialize(serialize(data)) = data

PennyWise Additions

  • New FinanceExport unit: JSON, CSV, XML export functions
  • Bank CSV importer with configurable column mapping
  • INI-based configuration file with TConfigManager
  • Auto-categorization of bank transactions by keyword matching