← back to roto on ba3a.dev

JsonCodec

abstract class JsonCodec<T>(serializer: KSerializer<T>, val fileName: String, error: (message: String, cause: Throwable) -> IOException)

Reads and writes one JSON file format; each format's codec is an object extending this.

Reading is strict: an unknown key (most often a typo), a missing field, or a value that fails the model's validation is an IOException from error with the reason, never a silent default. A leading byte-order mark, which some Windows editors add, is accepted. Writing is pretty-printed UTF-8 with \n line endings, so the same value always encodes to the same bytes, and a write to a Path goes through AtomicFile.

fileName is the name files of this format go by, for example roto.json; error messages use it too.

Inheritors

Constructors

Link copied to clipboard
constructor(serializer: KSerializer<T>, fileName: String, error: (message: String, cause: Throwable) -> IOException)

Properties

Link copied to clipboard

the name files of this format go by, such as roto.json

Functions

Link copied to clipboard
fun read(input: InputStream): T

Reads a value from input, which is read to its end but not closed.

fun read(path: Path): T

Reads the value stored at path.

Link copied to clipboard
fun write(value: T, output: OutputStream)

Writes value to output, which is not closed.

fun write(value: T, path: Path)

Writes value to path through AtomicFile, so a crash never leaves a half-written file.