Liking cljdoc? Tell your friends :D

clj-commons/clj-yaml

Provides YAML encoding and decoding for Clojure via the snakeyaml Java library.

Clojars Project cljdoc badge CircleCI Status bb built-in Slack chat

(This is a maintained fork of the circleci fork which forked from the original)

Usage

(require '[clj-yaml.core :as yaml])

(yaml/generate-string
  [{:name "John Smith", :age 33}
   {:name "Mary Smith", :age 27}])
"- {name: John Smith, age: 33}\n- {name: Mary Smith, age: 27}\n"

(yaml/parse-string "
- {name: John Smith, age: 33}
- name: Mary Smith
  age: 27
")
=> ({:name "John Smith", :age 33}
    {:name "Mary Smith", :age 27})

By default, keys are converted to clojure keywords. To prevent this, add :keywords false parameters to the parse-string function:

(yaml/parse-string "
- {name: John Smith}
" :keywords false)

Unknown tags can be handled by passing a handler function via the :unknown-tag-fn parameter. The handler function is provided a map which includes :tag and :value keys. Note that the value passed to the unknown-tag-fn is a string if it’s a scalar, regardless of the quoting (or lack thereof) of the scalar value.

;; drop tags
(yaml/parse-string "!Base12 10" :unknown-tag-fn :value
;; => "10"
(yaml/parse-string "!Base12 10"
                   :unknown-tag-fn (fn [{:keys [tag value]}]
                                      (if (= "!Base12" tag)
                                          (Integer/parseInt value 12)
                                          value)))
;; => 12

Different flow styles (:auto, :block, :flow) allow customization of how YAML is rendered:

(yaml/generate-string some-data :dumper-options {:flow-style :block})

Use the :indent (default: 2) and :indicator-indent (default: 0) options to adjust indentation:

(yaml/generate-string some-data :dumper-options {:indent 6
                                                 :indicator-indent 3
                                                 :flow-style :block})
=>
todo:
   -  name: Fix issue
      responsible:
            name: Rita

:indent must always be larger than :indicator-indent. If only 1 higher, the indicator will be on a separate line:

(yaml/generate-string some-data :dumper-options {:indent 2
                                                 :indicator-indent 1
                                                 :flow-style :block})
=>
todo:
 -
  name: Fix issue
  responsible:
    name: Rita

Installation

clj-commons/clj-yaml is available as a Maven artifact from Clojars.

Leiningen/Boot

[clj-commons/clj-yaml "1.1.48"]

Clojure CLI/deps.edn

clj-commons/clj-yaml {:mvn/version "1.1.48"}

Development

We use babashka tasks. To see a list of available tasks:

bb tasks
$ git clone git://github.com/clj-commons/clj-yaml.git
$ bb download-deps
$ bb compile-java
$ bb test

If you want more control over tests:

$ clojure -M:test <your cognitect test runner opts here>

Use as git dependency

To get the latest changes that are not yet released to Clojars, you can use this library as a git dependency:

$ cat deps.edn
{:deps {clj-commons/clj-yaml {:git/url "https://github.com/clj-commons/clj-yaml"
                              :git/sha "05eaca35c34c092ffdd2e620ef07962ce147a88b"}}}

$ clj -X:deps prep
$ clj -M -e "(require '[clj-yaml.core :as yaml])"

People

Founders

License

(c) Lance Bradley - Licensed under the same terms as clojure itself. See LICENCE file for details. Portions (c) Owain Lewis as marked.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close