glazer_yaml (glazer v0.5.7)
View SourceFast YAML encoding and decoding using the glaze C++ library.
By default nulls are represented as the atom null. To change it
application-wide, set the null env key in your config:
{glazer, [{null, nil}]}.Features
- Decoding YAML mappings/sequences/scalars to Erlang maps/lists/scalars, including big integers
- Encoding Erlang terms to YAML in block style
- Configurable representation of YAML
nulland mapping keys, with optional YAML 1.1 boolean compatibility (yes/no/on/off) read_file/1,2andwrite_file/2,3helpers for decoding/encoding directly to/from a file
Example
1> glazer_yaml:decode(<<"a: 1\nb:\n - true\n - null\n - 3.5\n">>).
#{<<"a">> => 1, <<"b">> => [true, null, 3.5]}
2> glazer_yaml:encode(#{<<"a">> => 1, <<"b">> => [true, null, 3.5]}).
<<"a: 1\nb:\n - true\n - null\n - 3.5\n">>See also [https://github.com/stephenberry/glaze]
Summary
Functions
Decode a YAML binary or iolist to an Erlang term. YAML mappings are returned
as maps (default). Raises {parse_error, Reason} on invalid input.
Decode a YAML binary or iolist to an Erlang term with options.
Raises {parse_error, Msg} on invalid input.
Encode an Erlang term to a YAML binary in block style (2-space indentation, sequences at the same indentation as the mapping key that owns them).
Encode an Erlang term to a YAML binary in block style with options.
Read Filename and decode its contents as YAML.
Read Filename and decode its contents as YAML, with decode options
(see decode/2).
Decode a YAML binary or iolist, returning {ok, Term} or
{error, Msg} instead of raising.
Decode a YAML binary or iolist with options, returning {ok, Term} or
{error, Msg} instead of raising.
Encode Data to YAML and write it to Filename, overwriting any existing
file.
Encode Data to YAML with encode options (see encode/2) and write it to
Filename, overwriting any existing file.
Types
-type decode_opt() :: use_nil | {null_term, atom()} | {keys, atom | existing_atom | binary} | yaml_1_1_bools.
-type decode_opts() :: [decode_opt()].
YAML decode options:
use_nil- use the atomnilfor YAMLnull/~/empty values{null_term, Atom}- useAtomfor YAMLnull/~/empty values{keys, atom}- decode mapping keys as atoms{keys, existing_atom}- decode mapping keys as existing atoms, fall back to binary{keys, binary}- decode mapping keys as binaries (default)yaml_1_1_bools- additionally treatyes/no/on/off(and case variants) as booleans, per the YAML 1.1 core schema. By default (YAML 1.2 core schema) onlytrue/falseare recognized as booleans.
-type encode_opt() :: use_nil | {null_term, atom()}.
-type encode_opts() :: [encode_opt()].
YAML encode options:
use_nil- treat the atomnilas YAMLnull{null_term, Atom}- treatAtomas YAMLnull
Functions
Decode a YAML binary or iolist to an Erlang term. YAML mappings are returned
as maps (default). Raises {parse_error, Reason} on invalid input.
-spec decode(binary() | iolist(), decode_opts()) -> term().
Decode a YAML binary or iolist to an Erlang term with options.
Raises {parse_error, Msg} on invalid input.
Example
1> glazer_yaml:decode(<<"a: ~\n">>, [use_nil]).
#{<<"a">> => nil}
2> glazer_yaml:decode(<<"a: 1\n">>, [{keys, atom}]).
#{a => 1}
3> glazer_yaml:decode(<<"a: yes\n">>, [yaml_1_1_bools]).
#{<<"a">> => true}
Encode an Erlang term to a YAML binary in block style (2-space indentation, sequences at the same indentation as the mapping key that owns them).
-spec encode(term(), encode_opts()) -> binary().
Encode an Erlang term to a YAML binary in block style with options.
Example
1> glazer_yaml:encode(#{<<"a">> => nil}, [use_nil]).
<<"a: null\n">>
-spec read_file(file:name_all()) -> term().
Read Filename and decode its contents as YAML.
Raises {parse_error, Reason} if the file's contents aren't valid YAML, or
a binary "Filename: Reason" message (see file:format_error/1) if the
file can't be read.
Example
1> glazer_yaml:read_file("data.yaml").
#{<<"a">> => 1}
-spec read_file(file:name_all(), decode_opts()) -> term().
Read Filename and decode its contents as YAML, with decode options
(see decode/2).
Decode a YAML binary or iolist, returning {ok, Term} or
{error, Msg} instead of raising.
-spec try_decode(binary() | iolist(), decode_opts()) -> {ok, term()} | {error, binary()}.
Decode a YAML binary or iolist with options, returning {ok, Term} or
{error, Msg} instead of raising.
-spec write_file(file:name_all(), term()) -> ok.
Encode Data to YAML and write it to Filename, overwriting any existing
file.
Raises a binary "Filename: Reason" message (see file:format_error/1)
if the file can't be written.
Example
1> glazer_yaml:write_file("data.yaml", #{<<"a">> => 1}).
ok
-spec write_file(file:name_all(), term(), encode_opts()) -> ok.
Encode Data to YAML with encode options (see encode/2) and write it to
Filename, overwriting any existing file.