Packages
toon_codec
1.0.0
TOON (Token-Oriented Object Notation) encoder/decoder - a compact, token-efficient format for LLM input
Current section
Files
Jump to
Current section
Files
src/toon_codec.erl
-module(toon_codec).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/toon_codec.gleam").
-export([encode/1, encode_with_options/2, decode/1, decode_with_options/2, default_encode_options/0, default_decode_options/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" TOON (Token-Oriented Object Notation) encoder/decoder for Gleam.\n"
"\n"
" TOON is a compact, human-readable format for LLM input that reduces\n"
" token usage compared to JSON while maintaining readability.\n"
"\n"
" This library provides encoding and decoding functions for converting\n"
" between JSON-like values and TOON format strings.\n"
"\n"
" ## Quick Start\n"
"\n"
" ```gleam\n"
" import toon_codec.{type JsonValue, Object, String, Number}\n"
"\n"
" pub fn main() {\n"
" // Create a JSON value\n"
" let user = Object([\n"
" #(\"name\", String(\"Alice\")),\n"
" #(\"age\", Number(30.0)),\n"
" #(\"active\", Bool(True)),\n"
" ])\n"
"\n"
" // Encode to TOON\n"
" let toon_str = toon_codec.encode(user)\n"
" // Result: \"name: Alice\\nage: 30\\nactive: true\"\n"
"\n"
" // Decode back to JSON\n"
" case toon_codec.decode(toon_str) {\n"
" Ok(decoded) -> // Process the decoded value\n"
" Error(err) -> // Handle the error\n"
" }\n"
" }\n"
" ```\n"
"\n"
" ## Custom Options\n"
"\n"
" ```gleam\n"
" import toon_codec.{EncodeOptions, Tab}\n"
"\n"
" let options = EncodeOptions(\n"
" indent: 4,\n"
" delimiter: Tab,\n"
" length_marker: HashMarker,\n"
" )\n"
"\n"
" let toon = toon_codec.encode_with_options(value, options)\n"
" ```\n"
" Encode a JSON value to TOON format with default options.\n"
"\n"
" Default options:\n"
" - indent: 2 spaces\n"
" - delimiter: comma\n"
" - length_marker: no marker\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let value = Object([\n"
" #(\"name\", String(\"Alice\")),\n"
" #(\"age\", Number(30.0)),\n"
" ])\n"
" encode(value)\n"
" // -> \"name: Alice\\nage: 30\"\n"
" ```\n"
" Encode a JSON value to TOON format with custom options.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let options = EncodeOptions(\n"
" indent: 4,\n"
" delimiter: Tab,\n"
" length_marker: HashMarker,\n"
" )\n"
" encode_with_options(value, options)\n"
" ```\n"
" Decode TOON format to JSON value with default options.\n"
"\n"
" Default options:\n"
" - indent: 2 spaces\n"
" - strict: true\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" decode(\"name: Alice\\nage: 30\")\n"
" // -> Ok(Object([#(\"name\", String(\"Alice\")), #(\"age\", Number(30.0))]))\n"
" ```\n"
" Decode TOON format to JSON value with custom options.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let options = DecodeOptions(indent: 4, strict: False)\n"
" decode_with_options(input, options)\n"
" ```\n"
" Get the default encoding options.\n"
"\n"
" Returns:\n"
" - indent: 2\n"
" - delimiter: Comma\n"
" - length_marker: NoMarker\n"
" Get the default decoding options.\n"
"\n"
" Returns:\n"
" - indent: 2\n"
" - strict: True\n"
).
-file("src/toon_codec.gleam", 136).
-spec encode(toon_codec@types:json_value()) -> binary().
encode(Value) ->
Options = toon_codec@types:default_encode_options(),
toon_codec@encode:encode_value(Value, Options).
-file("src/toon_codec.gleam", 141).
-spec encode_with_options(
toon_codec@types:json_value(),
toon_codec@types:encode_options()
) -> binary().
encode_with_options(Value, Options) ->
toon_codec@encode:encode_value(Value, Options).
-file("src/toon_codec.gleam", 147).
-spec decode(binary()) -> {ok, toon_codec@types:json_value()} |
{error, toon_codec@error:toon_error()}.
decode(Input) ->
Options = toon_codec@types:default_decode_options(),
toon_codec@decode:decode_value(Input, Options).
-file("src/toon_codec.gleam", 152).
-spec decode_with_options(binary(), toon_codec@types:decode_options()) -> {ok,
toon_codec@types:json_value()} |
{error, toon_codec@error:toon_error()}.
decode_with_options(Input, Options) ->
toon_codec@decode:decode_value(Input, Options).
-file("src/toon_codec.gleam", 161).
-spec default_encode_options() -> toon_codec@types:encode_options().
default_encode_options() ->
toon_codec@types:default_encode_options().
-file("src/toon_codec.gleam", 165).
-spec default_decode_options() -> toon_codec@types:decode_options().
default_decode_options() ->
toon_codec@types:default_decode_options().