Current section

Files

Jump to
gleam_json src gleam@json.erl
Raw

src/gleam@json.erl

-module(gleam@json).
-compile(no_auto_import).
-export([decode/2, to_string/1, to_string_builder/1, string/1, bool/1, int/1, float/1, null/0, nullable/2, object/1, array/2, preprocessed_array/1]).
-export_type([json/0, decode_error/0]).
-type json() :: any().
-type decode_error() :: unexpected_end_of_input |
{unexpected_byte, binary(), integer()} |
{unexpected_sequence, binary(), integer()} |
{unexpected_format, list(gleam@dynamic:decode_error())}.
-spec decode(
binary(),
fun((gleam@dynamic:dynamic()) -> {ok, I} |
{error, list(gleam@dynamic:decode_error())})
) -> {ok, I} | {error, decode_error()}.
decode(Json, Decoder) ->
case gleam_json_ffi:decode(Json) of
{error, _try} -> {error, _try};
{ok, Dynamic_value} ->
_pipe = Dynamic_value,
_pipe@1 = Decoder(_pipe),
gleam@result:map_error(
_pipe@1,
fun(A) -> {unexpected_format, A} end
)
end.
-spec to_string(json()) -> binary().
to_string(A) ->
gleam_json_ffi:json_to_string(A).
-spec to_string_builder(json()) -> gleam@string_builder:string_builder().
to_string_builder(A) ->
gleam_json_ffi:json_to_iodata(A).
-spec string(binary()) -> json().
string(A) ->
gleam_json_ffi:string(A).
-spec bool(boolean()) -> json().
bool(A) ->
gleam_json_ffi:bool(A).
-spec int(integer()) -> json().
int(A) ->
gleam_json_ffi:int(A).
-spec float(float()) -> json().
float(A) ->
gleam_json_ffi:float(A).
-spec null() -> json().
null() ->
gleam_json_ffi:null().
-spec nullable(gleam@option:option(O), fun((O) -> json())) -> json().
nullable(Input, Inner_type) ->
case Input of
{some, Value} ->
Inner_type(Value);
none ->
gleam_json_ffi:null()
end.
-spec object(list({binary(), json()})) -> json().
object(A) ->
gleam_json_ffi:object(A).
-spec array(list(R), fun((R) -> json())) -> json().
array(Entries, Inner_type) ->
_pipe = Entries,
_pipe@1 = gleam@list:map(_pipe, Inner_type),
gleam_json_ffi:array(_pipe@1).
-spec preprocessed_array(list(json())) -> json().
preprocessed_array(A) ->
gleam_json_ffi:array(A).