Copyright © 2012-2014 Yakaz, 2016-2022 Jean-Sébastien Pédron <jean-sebastien.pedron@dumbbell.fr>
Authors: Jean-Sébastien Pédron (jean-sebastien.pedron@dumbbell.fr).
YAML is a human-friendly data serialization format. The specification for this language and many examples are available from the Official YAML web site. You may also want to check the YAML Wikipedia article.
yamerl
is a pure Erlang application which is able
to parse YAML 1.1 and YAML 1.2 documents, as
well as JSON documents. It only depends
on standard Erlang/OTP applications; no external dependency is required.
It doesn't use native code either (neither port drivers nor NIFs). At
this time, it has no support to serialize a YAML document.
yamerl
is distributed under the terms of the 2-clause BSD
license; see COPYING
.
yamerl_constr:string("YAML snippet").
yamerl_constr:string("YAML snippet", [{schema, yaml11}]).
yamerl_constr:string(<<"JSON snippet">>, [{schema, json}]).
% Enable support for Erlang atoms. yamerl_app:set_param(node_mods, [yamerl_node_erlang_atom]), yamerl_constr:string("!<tag:yamerl,2012:atom> atom"). % Autodetect Erlang atoms in plain scalars. yamerl_app:set_param(node_mods, [yamerl_node_erlang_atom]), yamerl_constr:string("atom", [{erlang_atom_autodetection, true}]). % Atoms must already exist. yamerl_app:set_param(node_mods, [yamerl_node_erlang_atom]), yamerl_constr:string("atom", [ {erlang_atom_autodetection, true}, {erlang_atom_only_if_exist, true} ]).
% Enable support for Erlang fun(). yamerl_app:set_param(node_mods, [yamerl_node_erlang_fun]), [Plus_One_Fun] = yamerl_constr:string(<<"!<tag:yamerl,2012:fun> fun(X) -> X + 1 end.">>), Plus_One_Fun(2). % Return 3.
% Both calls return the same value. yaml:load_file("input.yaml", [{schema, yaml_schema_failsafe}]), yamerl_yamler_compat:load_file("input.yaml", [{schema, yaml_schema_failsafe}])
application:start(yamerl).
This is required so that application environment variables are available.
Now, you can use theyamerl_constr
module to parse and construct a
list of documents from:
Because a YAML input stream may contain multiple documents, yamerl_constr
always returns a list of documents, even if the input
stream only contains one.
application:start(yamerl).
Documents = yamerl_constr:string("Hello!"). % Documents is a list of constructed documents.
Documents = yamerl_constr:file("input.yaml"). % Documents is a list of constructed documents.
% Create a new construction state. The only required argument is an % arbitrary term describing the source of the data. Here, we use the % same term structure as yamerl_constr:file/{1, 2}. Constr_State = yamerl_constr:new({file, "<stdin>"}), % Feed the parser with binary chunks. The developer is responsible for % reading the chunk from the backing source. % % The function returns an updated construction state, which replaces the % previous one. % % yamerl_constr:next_chunk/2 can be called as many times as necessary. {continue, Constr_State2} = yamerl_constr:next_chunk(Constr_State, Chunk), % When the last chunk is reached, call yamerl_constr:last_chunk/2. Documents = yamerl_constr:last_chunk(Constr_State2, Last_Chunk). % Documents is a list of constructed documents.
See yamerl_constr
for more information.
yamerl throws an exception when an error occurs.
application:start(yamerl).
-include_lib("yamerl/include/yamerl_errors.hrl"). % ... try Documents = yamerl_constr:string("Hello!"), % Documents is a list of constructed documents. Documents catch throw:#yamerl_exception{errors = Errors} -> % Do something with the exception. Errors end.
#yamerl_exception{}
record embeds all encountered
errors:
#yamerl_exception{ errors = [] % List of errors. }.Errors are records where the two first members are always:
type
, either error
or warning
;text
, a human-readable error message.#yamerl_invalid_option{}
;#yamerl_parsing_error{}
.See yamerl_constr
for more information.
Generated by EDoc