Skip to content

GPrimola/yamel

Repository files navigation

Yamel - Yaml Serializer and Parser

CI Coverage Status Docs Hex.pm


An yaml parser and serializer to work with Yaml files in Elixir.

Installation

Add yamel to your list of dependencies in mix.exs:

def deps do
  [
    {:yamel, "~> 2.0.4"}
  ]
end

Usage

yaml_string = ~S"""
foo: bar
zoo:
  - caa
  - boo
  - vee
"""

Yamel.decode!(yaml_string) # equivalent to Yamel.decode!(yaml_string, keys: :string)
=> %{"foo" => "bar", "zoo" => ["caa", "boo", "vee"]}

Yamel.decode!(yaml_string, keys: :atom)
=> %{foo: "bar", zoo: ["caa", "boo", "vee"]}

Yamel.encode!(["caa", :boo, :"\"foo\""])
=> "- caa\n- boo\n- \"foo\"\n\n"

%{foo: :bar, zoo: :caa}
|> Yamel.encode!()
|> Yamel.IO.write!("/to/file.yml")
=> :ok

%{foo: value} = Yamel.IO.read!("/from/file.yaml")

Deriving

defmodule MyApp.MyStruct do
  defstruct [:field1, :field2, :field3, field4: true]
  @derive {Yamel.Encoder, only: [:field2, :field4]}
end