Parser for PostgreSQL array literals (array_out output).
Implementation inspired by Supabase Realtime and Sequin.
Parsing is STRICT about element separation (1.3.0): array_out never emits an
empty element, and Postgres rejects {a,,b} / {a,} / {,a} on input — so the
parser errors on them instead of silently skipping and delivering a wrong-length
list. Quoted elements may contain braces, commas, and escaped quotes; they are
framing-transparent (braces inside quotes are never structural).
Summary
Functions
Parses a PostgreSQL array literal string into an Elixir list.
Functions
Parses a PostgreSQL array literal string into an Elixir list.
Returns all elements as strings - the caller is responsible for any
type conversion. NULL values are returned as nil.
Parameters
array_string- PostgreSQL array literal (e.g.,"{1,2,3}")
Returns
{:ok, list}- Successfully parsed array as nested list{:error, reason}- Parsing failed with reason
Examples
# Simple arrays
Replicant.Casting.ArrayParser.parse("{1,2,3}")
{:ok, ["1", "2", "3"]}
# Empty arrays
Replicant.Casting.ArrayParser.parse("{}")
{:ok, []}
# Arrays with quoted strings
Replicant.Casting.ArrayParser.parse("{"hello, world","foo"}")
{:ok, ["hello, world", "foo"]}
# Nested arrays (braces inside quoted elements are not structural)
Replicant.Casting.ArrayParser.parse("{{\"a}b\"}}")
{:ok, [["a}b"]]}
# NULL values
Replicant.Casting.ArrayParser.parse("{1,NULL,3}")
{:ok, ["1", nil, "3"]}