MaybeJustJames / yaml / Yaml.Encode

Turn Elm values into YAML. You use Yaml.Encode in a very similar way to how you use Json.Encode. For an excellent introduction to encoding (with Json.Encode) have a look at this blog post.

Table of Contents


type Encoder

A value that knows how to encode Elm values into YAML.

Run Encoders

toString : Basics.Int -> Encoder -> String

Encode a given Elm value into a YAML formatted string.

The first argument specifies the amount of indentation in the resulting string.

toString 0 (int 4) --> "4"

toString 0 (list int [ 1, 2, 3 ]) --> "[1, 2, 3]"

toString 2 (list int [ 1, 2, 3 ])
--> "- 1\n- 2\n- 3"

You can also embed your encoded values into a YAML document:

toString 2 (document
              <| record [ ( "hello", string "world" ) ])
--> "---\nhello: world\n..."

Primitives

string : String -> Encoder

Encode a String into a YAML string.

toString 0 (string "") --> ""

toString 0 (string "hello") --> "hello"

int : Basics.Int -> Encoder

Encode an Int into a YAML int.

toString 0 (int 42) --> "42"

toString 0 (int -7) --> "-7"

toString 0 (int 0) --> "0"

float : Basics.Float -> Encoder

Encode a Float into a YAML float.

nan : Float
nan = (0/0)

infinity : Float
infinity = (1/0)

toString 0 (float 3.14)      --> "3.14"

toString 0 (float -42)       --> "-42"

toString 0 (float 0.0)       --> "0"

toString 0 (float nan)       --> ".nan"

toString 0 (float -infinity) --> "-.inf"

bool : Basics.Bool -> Encoder

Encode a Bool into a YAML bool.

toString 0 (bool True) --> "true"

toString 0 (bool False) --> "false"

null : Encoder

Encode a YAML null value

toString 0 null --> "null"

toString 2 (record [ ("null", null) ])
--> "null: null"

value : Yaml.Parser.Ast.Value -> Encoder

Encode a Value as produced by Yaml.Decode.value

Data Structures

list : (a -> Encoder) -> List a -> Encoder

Encode a List into a YAML list.

toString 0 (list float [1.1, 2.2, 3.3])
--> "[1.1, 2.2, 3.3]"

toString 2 (list string ["a", "b"])
--> "- a\n- b"

record : List ( String, Encoder ) -> Encoder

Encode a YAML record.

toString 0 (record [ ( "name", string "Sally" )
                   , ( "height", int 187)
                   ]
           )
--> "{name: Sally, height: 187}"

toString 2 (record [ ( "foo", int 42 )
                   , ( "bar", float 3.14 )
                   ]
           )
--> "foo: 42\nbar: 3.14"

dict : (k -> String) -> (v -> Encoder) -> Dict k v -> Encoder

Encode a Dict into a YAML record.

import Dict


toString 0 (dict
              identity
              int (Dict.singleton "Sue" 38))
--> "{Sue: 38}"

toString 2 (dict
              identity
              string (Dict.fromList [ ("hello", "foo")
                                    , ("world", "bar")
                                    ]
                     )
           )
--> "hello: foo\nworld: bar"

YAML specific details

document : Encoder -> Encoder

Encode a YAML document

YAML "documents" are demarked by "---" at the beginning and "..." at the end. This encoder places a value into a demarcated YAML document.

toString 0 (document <| string "hello")
--> "---\nhello\n..."

toString 2 (document
              <| record [ ("hello", int 5)
                        , ("foo", int 3)
                        ]
           )
--> "---\nhello: 5\nfoo: 3\n..."