fio/json

Types

Error returned by read_json when either the file I/O fails or the caller-supplied decoder rejects the content.

pub type JsonError(decode_err) {
  IoError(error: error.FioError)
  ParseError(error: decode_err)
}

Constructors

  • IoError(error: error.FioError)

    The file could not be read (e.g. missing, permission denied).

  • ParseError(error: decode_err)

    The file was read successfully but the decoder rejected the content.

Values

pub fn read_json(
  path: String,
  decoder: fn(String) -> Result(a, e),
) -> Result(a, JsonError(e))

Read a file and decode its content with decoder.

Returns Error(IoError(_)) on I/O failure and Error(ParseError(_)) when the decoder rejects the content.

fjson.read_json("settings.json", my_decoder)
// Ok(settings) | Error(IoError(Enoent)) | Error(ParseError(...))
pub fn write_json_atomic(
  path: String,
  value: a,
  encoder: fn(a) -> String,
) -> Result(Nil, error.FioError)

Encode value with encoder and write the result atomically to path.

Uses fio.write_atomic under the hood, so readers never observe partial content.

fjson.write_json_atomic("settings.json", settings, my_encoder)
Search Document