Encoding of TOON arrays in three formats:
- Inline: for primitive arrays (e.g., tags[2]: reading,gaming)
- Tabular: for uniform object arrays (e.g., users[2]{name,age}: Alice,30 / Bob,25)
- List: for mixed or non-uniform arrays
Summary
Functions
Encodes an array with the given key.
Encodes an empty array.
Encodes a primitive array in inline format.
Encodes an array in list format (for mixed or non-uniform arrays).
Encodes a uniform object array in tabular format.
Functions
@spec encode(String.t(), list(), non_neg_integer(), map()) :: [iodata()]
Encodes an array with the given key.
Automatically detects the appropriate format based on array contents.
Encodes an empty array.
Examples
iex> result = ToonEx.Encode.Arrays.encode_empty("items", nil)
iex> IO.iodata_to_binary(result)
"items[0]:"
Encodes a primitive array in inline format.
Examples
iex> opts = %{delimiter: ",", length_marker: nil}
iex> result = ToonEx.Encode.Arrays.encode_inline("tags", ["reading", "gaming"], opts)
iex> IO.iodata_to_binary(result)
"tags[2]: reading,gaming"
@spec encode_list(String.t(), list(), non_neg_integer(), map()) :: [iodata()]
Encodes an array in list format (for mixed or non-uniform arrays).
Returns a list where the first element is the header, and subsequent elements are list items (without base indentation - indentation is added by the Writer).
Examples
iex> opts = %{delimiter: ",", length_marker: nil, indent_string: " "}
iex> items = [%{"title" => "Book", "price" => 9}, %{"title" => "Movie", "duration" => 120}]
iex> [header | list_items] = ToonEx.Encode.Arrays.encode_list("items", items, 0, opts)
iex> IO.iodata_to_binary(header)
"items[2]:"
iex> Enum.map(list_items, &IO.iodata_to_binary/1)
["- price: 9", " title: Book", "- duration: 120", " title: Movie"]
@spec encode_tabular(String.t(), list(), non_neg_integer(), map()) :: [iodata()]
Encodes a uniform object array in tabular format.
Returns a list where the first element is the header, and subsequent elements are data rows (without indentation - indentation is added by the Writer).
Examples
iex> opts = %{delimiter: ",", length_marker: nil, indent_string: " "}
iex> users = [%{"name" => "Alice", "age" => 30}, %{"name" => "Bob", "age" => 25}]
iex> [header | rows] = ToonEx.Encode.Arrays.encode_tabular("users", users, 0, opts)
iex> IO.iodata_to_binary(header)
"users[2]{age,name}:"
iex> Enum.map(rows, &IO.iodata_to_binary/1)
["30,Alice", "25,Bob"]