YamlRustler (YamlRustler v0.2.2)

Copy Markdown View Source

YAML parsing library using Rust's yaml-rust2 via Rustler.

Summary

Functions

Parses a YAML string and returns an Elixir data structure.

Functions

parse(yaml_string, opts \\ [])

@spec parse(binary(), keyword()) :: {:ok, term()} | {:error, binary()}

Parses a YAML string and returns an Elixir data structure.

Only the first document is returned, and input without any document (an empty string or only comments) returns {:ok, nil}. The YAML floats .inf, -.inf and .nan cannot be represented on the BEAM and are returned as the atoms :infinity, :neg_infinity and :nan.

Options

  • :ruby - read plain values the way Ruby's Psych does, for YAML written by Ruby (e.g. Rails serialized columns). Psych follows YAML 1.1, where a float needs a . and an exponent needs a sign, so it writes strings like 1e5 or 1.5e3 without quotes. With this option they stay strings instead of becoming floats. Defaults to false (YAML 1.2).

Examples

iex> YamlRustler.parse("foo: bar")
{:ok, %{"foo" => "bar"}}

iex> YamlRustler.parse("invalid: : yaml")
{:error, "YAML parsing error: mapping values are not allowed in this context at byte 9 line 1 column 10"}

iex> YamlRustler.parse("a: 1e5")
{:ok, %{"a" => 100000.0}}

iex> YamlRustler.parse("a: 1e5", ruby: true)
{:ok, %{"a" => "1e5"}}