MaybeJustJames/yaml/1.0.0

NOTE: the latest version is 2.1.7

YAML in Elm

Convert between type-safe Elm values and YAML.

This is forked from terezka/yaml.

Install

$ elm install MaybeJustJames/yaml

and import the library in an elm file like this

import Yaml.Decode -- for decoders

Documentation

Find the documentation on Elm's package website.

Example Usage

Say you have some YAML which looks like this:

---
- name:
    first: Marie
    last: Curie
  occupation: [ chemist, physicist ]
  age: 66
  children: [ Irène, Ève ]
- name:
    first: Alva
    last: Myrdal
  occupation: [ sociologist, diplomat, politician ]
  age: 84
  children: []
- name:
    first: Svetlana
    last: Alexievich
  occupation: [ journalist, historian ]
  age: 72
  children: []
...  

to decode this, you could write

import Yaml.Decode (..)

type alias Woman =
  { name : String
  , occupation : List String
  , age : Int
  , children : Int -- number of children
  }

decoder : Decoder Woman
decoder =
  map4 Woman
    (map2 (\first last -> first ++ " " ++ last)
          (at ["name", "first"] string)
          (at ["name", "last"] string))
    (field "occupation" (list string))
    (field "age" int)
    (map List.length (field "children" (list string)))


fromString
  (list decoder)
  yamlString -- The string containing the YAML example above

Development

The branch parser-logging contains a version of the parser logger by @Janiczek.

This, along with writing detailed tests using elm-test is how I've been developing this package.

Please feel encouraged and welcome to submit bugs, PRs, etc.

Major Missing Features

  • Yaml.Encode to encode Elm values into YAML.
  • Testing against the official YAML test suite.

Copying

You are free to copy, modify, and distribute this package with attribution under the terms of the MIT license. See the LICENSE file for details.