Current section
Files
Jump to
Current section
Files
src/postgleam@codec@jsonb.erl
-module(postgleam@codec@jsonb).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/postgleam/codec/jsonb.gleam").
-export([encode/1, decode/1, matcher/0]).
-file("src/postgleam/codec/jsonb.gleam", 25).
-spec encode(postgleam@value:value()) -> {ok, bitstring()} | {error, binary()}.
encode(Val) ->
case Val of
{jsonb, S} ->
{ok, <<1, S/binary>>};
_ ->
{error, <<"jsonb codec: expected Jsonb value"/utf8>>}
end.
-file("src/postgleam/codec/jsonb.gleam", 32).
-spec decode(bitstring()) -> {ok, postgleam@value:value()} | {error, binary()}.
decode(Data) ->
case Data of
<<1, Json_data/bitstring>> ->
case gleam@bit_array:to_string(Json_data) of
{ok, S} ->
{ok, {jsonb, S}};
{error, _} ->
{error, <<"jsonb codec: invalid UTF-8"/utf8>>}
end;
_ ->
{error,
<<"jsonb codec: missing version byte or invalid data"/utf8>>}
end.
-file("src/postgleam/codec/jsonb.gleam", 21).
-spec build(integer()) -> postgleam@codec:codec().
build(Type_oid) ->
{codec, <<"jsonb"/utf8>>, Type_oid, binary, fun encode/1, fun decode/1}.
-file("src/postgleam/codec/jsonb.gleam", 11).
-spec matcher() -> postgleam@codec:codec_matcher().
matcher() ->
{codec_matcher, <<"jsonb"/utf8>>, [3802], none, binary, fun build/1}.