CSV Standard Supported Types & Data Pipeline

Namespace: Canley.Utility.CSV.Standard
Class: CSVTable<T>

Overview

When hydrating records from a CSV file via reflection, CSVTable<T> validates field types to ensure robust parsing and prevent data corruption. The IsSupportedType method governs which types are automatically mapped from raw string data.

Definition

public static bool IsSupportedType(Type type)
  • Description: Evaluates whether a given C# or Unity type is supported natively by the CSV parsing and hydration pipeline.
  • Parameters:
    • type: The Type object of the field being evaluated.
  • Returns: bool - Returns true if the type is supported for automatic mapping; false otherwise.

Supported Type Categories

The hydration engine natively handles the following categories:

  1. Primitives & Basics:

    • bool (mapped via “1”, “true”, or case-insensitive string parsing)
    • Numeric primitives (int, float, double, long, etc.)
    • string
    • decimal
    • Enum types (parsed via string name or numeric value)
  2. Unity Specific Structs:

    • Vector3 (parsed from comma-separated coordinate strings)
    • Vector2 (parsed from comma-separated coordinate strings)
    • Color (parsed via HTML hex color strings)
    • Quaternion (parsed via 4-component raw strings or 3-component Euler angles based on rotationMode settings)

Hooks & Shadow Dictionary Pipeline

Beyond standard primitive mapping, the CSV architecture includes built-in mechanisms to handle custom data transformations and preserve columns that do not map directly to C# fields.

1. The Shadow Dictionary (rawValues)

Every record instance maintains a dictionary (rawValues) that captures every single column present in the source CSV file during import, regardless of whether a matching public C# field exists.

  • Preservation: Unmapped columns or custom data columns are safely stored here during hydration.
  • Export Round-Tripping: When GetRawGrid or SaveTable is called, these shadow values are merged back into the export data, ensuring that third-party spreadsheet columns or game metadata aren’t wiped out when saving files back to disk.

2. OnImportField Hook

Before a raw string from a CSV cell is applied to a C# field or stored in the shadow dictionary, it passes through the virtual OnImportField method defined on CSVRecord.

  • Purpose: Allows individual record subclasses to intercept, sanitize, or transform incoming cell strings on the fly.
  • Example Use Case: Stripping currency symbols, decrypting values, or handling legacy naming variations before hydration occurs.

3. OnExportField Hook

During the export phase, right before rows are compiled into the final string grid, the engine invokes the virtual OnExportField method.

  • Purpose: Gives record instances absolute control over outgoing values on a per-column basis.

  • Example Use Case: Formatting specific numerical fields into custom string representations or serializing complex states back into specific string formats.

  • Example:

public class CustomItemRecord : CSVRecord
{
    public string itemName;
    public float basePrice;
 
    public override string OnImportField(string header, string rawValue)
    {
        // Custom preprocessing during import
        if (header == "basePrice" && rawValue.StartsWith("$"))
        {
            return rawValue.TrimStart('$');
        }
        return baseValue;
    }
 
    public override string OnExportField(string header, string currentValue)
    {
        // Custom postprocessing during export
        if (header == "basePrice")
        {
            return "$" + currentValue;
        }
        return currentValue;
    }
}

TIP

Use the shadow dictionary to safely carry forward extra columns from designer spreadsheets, and override OnImportField or OnExportField when you need specialized text formatting outside standard primitive parsing.