//////////////////////////////////////////

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.

//////////////////////////////////////////

= Processing CSV

Groovy has an optional `groovy-csv` module which provides support for reading and writing
https://datatracker.ietf.org/doc/html/rfc4180[CSV (RFC 4180)] data. The classes are found
in the `groovy.csv` package.

[[csv_csvslurper]]
== CsvSlurper

`CsvSlurper` parses CSV text into a list of maps, where each row becomes a map keyed
by the column headers from the first row. Values are returned as strings.

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=parse_text,indent=0]
----

Rows support dynamic property access using the header names:

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=property_access,indent=0]
----

=== Configuration

The separator character and quote character can be customised:

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=custom_separator,indent=0]
----

Quoted fields follow RFC 4180 — fields containing the separator, newlines, or the quote
character are enclosed in quotes, with embedded quotes doubled:

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=quoted_fields,indent=0]
----

=== CSV without a header row

When the CSV has no header row, call `setUseHeader(false)`. Each row is returned
as a map keyed by generated column names `c1`, `c2`, and so on:

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=no_header,indent=0]
----

Alternatively, supply explicit column names with `setColumns`:

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=explicit_columns,indent=0]
----

When explicit column names are supplied and `useHeader` remains true, the first
row is still consumed as a header row but the supplied names take precedence
over the names it contains.

=== Typed parsing

`CsvSlurper` can parse CSV directly into typed objects using Jackson databinding.
Standard Jackson annotations such as `@JsonProperty` and `@JsonFormat` are supported
for column name mapping and type conversion.
This is particularly useful for CSV since all values are strings — Jackson handles
the conversion to numeric, date, and other types automatically:

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=typed_parsing,indent=0]
----

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=typed_parsing_usage,indent=0]
----

Headerless CSV can also be parsed into typed objects. Positional columns are
derived from the target type's properties; use `@JsonPropertyOrder` to control
the expected column order:

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=typed_no_header,indent=0]
----

[source,groovy]
----
include::../test/groovy/csv/CsvSlurperTest.groovy[tags=typed_no_header_usage,indent=0]
----

[NOTE]
====
CSV has no native types — every cell is text, so the untyped `parse`/`parseText`
API always returns `String` values, including for date/time-looking columns. Use
the typed `parseAs` API with `java.time.*` fields (see <<csv_typed_temporal>>) for
`java.time.LocalDate`, `java.time.LocalTime`, `java.time.LocalDateTime`, and
`java.time.OffsetDateTime` fidelity.
====

[[csv_csvbuilder]]
== CsvBuilder

`CsvBuilder` converts collections of maps or typed objects to CSV. The keys of the first
map are used as column headers.

[source,groovy]
----
include::../test/groovy/csv/CsvBuilderTest.groovy[tags=to_csv_maps,indent=0]
----

=== Typed writing

`CsvBuilder` can also write typed objects. Jackson annotations are supported for
column naming and formatting:

[source,groovy]
----
include::../test/groovy/csv/CsvBuilderTest.groovy[tags=typed_writing,indent=0]
----

[source,groovy]
----
include::../test/groovy/csv/CsvBuilderTest.groovy[tags=typed_writing_usage,indent=0]
----

Columns for records appear in component declaration order:

[source,groovy]
----
include::../test/groovy/csv/CsvBuilderTest.groovy[tags=record_column_order,indent=0]
----

[source,groovy]
----
include::../test/groovy/csv/CsvBuilderTest.groovy[tags=record_column_order_usage,indent=0]
----

For other classes the column order is alphabetical. Use `@JsonPropertyOrder`
on the type to set an explicit column order.

=== Round-trip

CSV written by `CsvBuilder` can be read back with `CsvSlurper`:

[source,groovy]
----
include::../test/groovy/csv/CsvBuilderTest.groovy[tags=round_trip,indent=0]
----

[[csv_typed_temporal]]
=== Typed date and time values

When a target type declares `java.time.*` fields, `CsvBuilder.toCsv` and the typed
`CsvSlurper.parseAs` API round-trip temporal values with full fidelity:

[source,groovy]
----
include::../test/groovy/csv/CsvBuilderTest.groovy[tags=temporal_class,indent=0]
----

[source,groovy]
----
include::../test/groovy/csv/CsvBuilderTest.groovy[tags=temporal_typed,indent=0]
----
