Package groovy.csv

# Class CsvBuilder

java.lang.Object
groovy.csv.CsvBuilder

All Implemented Interfaces:
  `Writable`

@Incubating public class CsvBuilder extends Object implements Writable
Builds CSV output from collections of maps or typed objects.

Example with maps:

     def data = [[name: 'Alice', age: 30], [name: 'Bob', age: 25]]
     def csv = groovy.csv.CsvBuilder.toCsv(data)
     assert csv.contains('name,age')
     assert csv.contains('Alice,30')

Since:
  6.0.0

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CsvBuilder()`
  Creates a builder that uses comma-separated columns and double quotes for field escaping.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `CsvBuilder`
  `call(Collection<? extends Map<String,?>> data)`
  Build CSV from a collection of maps.
  `CsvBuilder`
  `setQuoteChar(char quoteChar)`
  Set the quote character (default: double-quote).
  `CsvBuilder`
  `setSeparator(char separator)`
  Set the column separator character (default: comma).
  `static String`
  `toCsv(Collection<? extends Map<String,?>> data)`
  Convert a collection of maps to CSV.
  `static <T> String`
  `toCsv(Collection<T> data, Class<T> type)`
  Convert a collection of typed objects to CSV using Jackson databinding.
  `String`
  `toString()`
  Returns the CSV content built so far, or an empty string if no data has been written yet.
  `Writer`
  `writeTo(Writer out)`
  Writes the current CSV content to the supplied writer.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### CsvBuilder

    public CsvBuilder()
    Creates a builder that uses comma-separated columns and double quotes for field escaping.

- ## Method Details

  - ### setSeparator

    public CsvBuilder setSeparator(char separator)
    Set the column separator character (default: comma).
    Parameters:
      `separator` - the separator character
    Returns:
      this builder for chaining

  - ### setQuoteChar

    public CsvBuilder setQuoteChar(char quoteChar)
    Set the quote character (default: double-quote).
    Parameters:
      `quoteChar` - the quote character
    Returns:
      this builder for chaining

  - ### toCsv

    public static String toCsv(Collection<? extends Map<String,?>> data)
    Convert a collection of maps to CSV. The keys of the first map are used as column headers.
    Parameters:
      `data` - the collection of maps
    Returns:
      the CSV string

  - ### toCsv

    public static <T> String toCsv(Collection<T> data, Class<T> type)
    Convert a collection of typed objects to CSV using Jackson databinding. Supports `@JsonProperty` and `@JsonFormat` annotations. Columns for records appear in component declaration order; for other classes the order is alphabetical. Use `@JsonPropertyOrder` on the type to set an explicit column order.
    Type Parameters:
      `T` - the object type
    Parameters:
      `data` - the collection of objects
      `type` - the object type (used to derive the schema)
    Returns:
      the CSV string

  - ### call

    public CsvBuilder call(Collection<? extends Map<String,?>> data)
    Build CSV from a collection of maps.
    Parameters:
      `data` - the collection of maps
    Returns:
      this builder

  - ### toString

    public String toString()
    Returns the CSV content built so far, or an empty string if no data has been written yet.
    Overrides:
      `toString` in class `Object`
    Returns:
      the current CSV content

  - ### writeTo

    public Writer writeTo(Writer out) throws IOException
    Writes the current CSV content to the supplied writer.
    Specified by:
      `writeTo` in interface `Writable`
    Parameters:
      `out` - the destination writer
    Returns:
      the writer passed in
    Throws:
      `IOException` - if the writer cannot be written to