TomlEncoder

class TomlEncoder

Describes how Kotlin model types are encoded into TomlValues. This API closely mirrors that of TomlDecoder.


If you intend to both encode and decode TOML, can get by with custom mappings, and don't need the full power of custom encoding functions, you should probably use TomlTranscoder instead.

Types

Companion
Link copied to clipboard
object Companion

Functions

pass
Link copied to clipboard
fun pass(): Nothing

Called by an encoder function to indicate that it can't encode the given value, and that the next encoder function for the source type should be given a chance instead.

with
Link copied to clipboard
fun with(vararg encoderFunctions: Pair<KClass<*>, TomlEncoder.(kotlinValue: Any) -> TomlValue>): TomlEncoder

Returns a copy of the target TOML encoder, extended with zero or more additional custom encoder functions. A custom encoder function is a function from some Kotlin value to a TomlValue. Custom encoder functions are associated with a KClass representing the source type.

inline fun <T> with(crossinline encoderFunction: TomlEncoder.(T) -> TomlValue): TomlEncoder

Returns a copy of the receiver TOML encoder extended with a single custom encoder function, without having to manually specify its target type.

withMapping
Link copied to clipboard
inline fun <T : Any> withMapping(vararg mapping: Pair<KotlinName, TomlName>): TomlEncoder

Returns a copy of the receiver TOML encoder, extended with a custom property mapping for the type T. Mappings are given on the form "kotlinName" to "tomlName".

fun <T : Any> withMapping(kClass: KClass<T>, vararg mapping: Pair<KotlinName, TomlName>): TomlEncoder

Returns a copy of the receiver TOML encoder, extended with a custom property mapping for the type indicated by the given KClass. Mappings are given on the form "kotlinName" to "tomlName".

Extensions

encode
Link copied to clipboard
fun TomlEncoder.encode(value: Any): TomlValue

Encodes the given Kotlin value to as into a TomlValue using the receiver TomlEncoder. If the value can't be encoded, a TomlException.EncodingError is thrown.