Csv
ceramic.Csv (Class)
CSV parsing and generation utilities with proper escaping and quote handling.
This class provides robust CSV parsing that handles:
- Automatic delimiter detection (comma or semicolon)
- Quoted values with embedded delimiters
- Escaped quotes within quoted values
- Multi-line values within quotes
- Dynamic field discovery from data
Features
- Auto-detection: Automatically detects comma or semicolon delimiters
- Proper Escaping: Handles quoted values and escaped quotes correctly
- Type-safe: Returns typed DynamicAccess objects for easy field access
- Flexible Output: Can generate CSV with specific or auto-discovered fields
Usage Examples
// Parse CSV string
var csv = 'name,age,city\n"John",30,"New York"\n"Jane",25,"San Francisco"';
var data = Csv.parse(csv);
trace(data[0].name); // "John"
// Generate CSV from objects
var items = [
{name: "Alice", score: 95},
{name: "Bob", score: 87}
];
var csvString = Csv.stringify(items);
See: ceramic.DatabaseAsset For loading CSV files as assets
Static Members
parse(csv: String): Array<haxe.DynamicAccess<String>>
Parses a CSV string into an array of objects.
Each row (except the header) becomes an object where field names are taken from the first row. The parser automatically detects whether commas or semicolons are used as delimiters.
CSV Format Rules
- First row must contain field names
- Values containing delimiters or newlines must be quoted
- Quotes within quoted values must be escaped as ""
- Trailing/leading whitespace is preserved
- Empty values are returned as empty strings
Name | Type | Description |
---|---|---|
csv |
String | The CSV string to parse |
Returns | Description |
---|---|
Array<haxe.DynamicAccess<String>> | Array of objects with fields as defined in the header row * haxe var csv = 'id,name,description\n' + '1,"Smith, John","Says ""Hello"""\n' + '2,Jane Doe,Regular description'; * var data = Csv.parse(csv); trace(data[0].name); // "Smith, John" trace(data[0].description); // "Says "Hello"" |
Converts an array of objects to CSV format.
Automatically handles escaping of special characters:
- Values containing commas, quotes, or newlines are wrapped in quotes
- Quotes within values are escaped as double quotes ("")
- Field order is determined by the fields parameter or auto-discovered
Name | Type | Default | Description |
---|---|---|---|
items |
Array<Dynamic> | Array of objects to convert to CSV | |
fields |
Array<String> | (optional) | Optional array of field names to include in specific order. If not provided, fields are auto-discovered from all objects. |
Returns | Description |
---|---|
String | CSV string with header row and data rows * haxe var data = [ {id: 1, name: "John", note: "Says "Hi""}, {id: 2, name: "Jane, MD", note: "Multi\nline"} ]; * // Auto-discover fields var csv1 = Csv.stringify(data); * // Specific fields only var csv2 = Csv.stringify(data, ["id", "name"]); |
Private Members
Internal warning function that adapts to the current platform. Uses ceramic logging when available, otherwise falls back to platform-specific console output.
Name | Type |
---|---|
str |
String |