toon_codec/encode/primitives

Primitive value encoding and header formatting.

This module handles encoding of primitive JSON values (strings, numbers, booleans, null) as well as formatting array headers. Encode a primitive JSON value to its TOON string representation.

Applies quoting rules based on the active delimiter context.

Examples

encode_primitive(Null, Comma)
// -> "null"

encode_primitive(Bool(True), Comma)
// -> "true"

encode_primitive(Number(42.0), Comma)
// -> "42"

encode_primitive(JsonString("hello"), Comma)
// -> "hello"

Encode a number, removing unnecessary decimals and normalizing -0 to 0. Encode a string value, applying quoting rules based on delimiter. Encode a key for use in key-value pairs.

Applies quoting rules for keys: keys must be quoted unless they match the pattern ^[A-Za-z_][\w.]*$

Examples

encode_key("name")
// -> "name"

encode_key("user-id")
// -> "\"user-id\""

Format an array header line.

Arguments

Examples

format_header(3, None, Comma, NoMarker)
// -> "[3]:"

format_header(3, Some("tags"), Comma, HashMarker)
// -> "tags[#3]:"

format_header(2, Some("items"), Tab, NoMarker)
// -> "items[2\t]:"

Format a tabular array header with field names.

Examples

format_tabular_header(2, Some("users"), ["id", "name"], Comma, NoMarker)
// -> "users[2]{id,name}:"

Convert delimiter to its header representation.

Comma has no representation (empty string). Tab and Pipe appear as their literal characters. Encode and join primitive values with a delimiter.

Used for inline primitive arrays.

Examples

let values = [Number(1.0), Number(2.0), Number(3.0)]
encode_and_join_primitives(values, Comma)
// -> "1,2,3"

Values

pub fn encode_and_join_primitives(
  values: List(types.JsonValue),
  delimiter: types.Delimiter,
) -> String
pub fn encode_key(key: String) -> String
pub fn encode_primitive(
  value: types.JsonValue,
  delimiter: types.Delimiter,
) -> String
pub fn format_header(
  length: Int,
  key: option.Option(String),
  delimiter: types.Delimiter,
  marker: types.LengthMarker,
) -> String
pub fn format_tabular_header(
  length: Int,
  key: option.Option(String),
  fields: List(String),
  delimiter: types.Delimiter,
  marker: types.LengthMarker,
) -> String
Search Document