TOML 1.0
The ShapeShift.Toml package maps PolyType contracts to TOML 1.0 (Tom's Obvious, Minimal Language).
It parses TOML 1.0 through Tomlyn's syntax tree and emits canonical TOML 1.0. TOML 1.1 extensions that 1.0 does not allow (\x and \e escapes, newlines and trailing commas in inline tables, and times that omit seconds) are rejected.
Install the ShapeShift.Toml package and annotate serialized root types with PolyType's GenerateShapeAttribute:
TomlSerializer serializer = new();
Person person = new("Ada", ["mathematics", "programming"]);
string toml = serializer.Serialize(person);
Person? copy = serializer.Deserialize<Person>(toml);
TomlSerializer supports:
- Serialization to and from TOML strings.
- Standard
[table]sections,[[array-of-tables]]sections, dotted and quoted keys, inline tables, and heterogeneous arrays. - Comments, multiline basic and literal strings, numeric bases, special floating-point values, and all TOML date/time forms when reading.
- All shared ShapeShift converters and policies, including naming policies, generated surrogates, attributed unions, default-value omission, and strict duplicate/required-member validation.
- Trimmed and NativeAOT applications without reflection-based TOML object mapping.
For example, nested objects and lists of objects produce Cargo-style sections:
[package]
name = "shape-shift"
version = "1.0.0"
[dependencies]
serde = { version = "1", features = ["derive"] }
[[bin]]
name = "shape-shift"
path = "src/main.rs"
Wire representations
TOML tables are encoded as maps with nested keys. Arrays are encoded as vectors. Scalar types are written in their native TOML representations:
- Booleans:
trueorfalse(lowercase). - Integers: Signed 64-bit values. The reader accepts decimal, hexadecimal, octal, and binary TOML forms; the writer emits decimal.
- Floats: IEEE 754 binary64 values. The reader accepts TOML decimal and exponent forms; the writer also emits
nan,inf, and-infwhen appropriate. - Strings: The reader accepts basic, literal, and multiline strings. The writer emits escaped basic strings.
- Dates and times: The reader accepts offset date-time, local date-time, local date, and local time values.
DateTimeproperties are read asDateTimevalues. UntypedShapeShiftValuereads return the canonical date text, because the dynamic model has no date type.DateTimevalues are written with round-trip precision.
TOML limitations
Unlike JSON or MessagePack, TOML:
- Has no null, binary, or duration scalar. Null object properties are omitted; null array elements and other unsupported values are rejected.
- Always has a table at the document root. Bare scalar or array documents are rejected.
- Limits integers to signed 64-bit values and floating-point values to binary64.
- Distinguishes strings from numbers and booleans by quoting. Unquoted
true,false, or numeric-looking strings will be parsed as their respective types. - Cannot represent every
ShapeShiftValue, because that type includes null and binary values.
Reader security
Tomlyn validates TOML 1.0 syntax, duplicate keys, and table redefinitions before ShapeShift begins conversion. Its nesting limit follows StartingContext.MaxDepth, and a parse failure is reported as a decoder error. ShapeShift then enforces missing required constructor arguments and the shared depth and security limits.