-module(fio@json). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/fio/json.gleam"). -export([read_json/2, write_json_atomic/3]). -export_type([json_error/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type json_error(FDO) :: {io_error, fio@error:fio_error()} | {parse_error, FDO}. -file("src/fio/json.gleam", 49). ?DOC( " Read a file and decode its content with `decoder`.\n" "\n" " Returns `Error(IoError(_))` on I/O failure and\n" " `Error(ParseError(_))` when the decoder rejects the content.\n" "\n" " ```gleam\n" " fjson.read_json(\"settings.json\", my_decoder)\n" " // Ok(settings) | Error(IoError(Enoent)) | Error(ParseError(...))\n" " ```\n" ). -spec read_json(binary(), fun((binary()) -> {ok, FDP} | {error, FDQ})) -> {ok, FDP} | {error, json_error(FDQ)}. read_json(Path, Decoder) -> case fio:read(Path) of {error, E} -> {error, {io_error, E}}; {ok, Content} -> case Decoder(Content) of {error, E@1} -> {error, {parse_error, E@1}}; {ok, Value} -> {ok, Value} end end. -file("src/fio/json.gleam", 71). ?DOC( " Encode `value` with `encoder` and write the result atomically to `path`.\n" "\n" " Uses `fio.write_atomic` under the hood, so readers never observe\n" " partial content.\n" "\n" " ```gleam\n" " fjson.write_json_atomic(\"settings.json\", settings, my_encoder)\n" " ```\n" ). -spec write_json_atomic(binary(), FDW, fun((FDW) -> binary())) -> {ok, nil} | {error, fio@error:fio_error()}. write_json_atomic(Path, Value, Encoder) -> fio:write_atomic(Path, Encoder(Value)).