Skip to content

A fast, idiomatic and easy to use Clojure YAML library. Based on Snake YAML

License

Notifications You must be signed in to change notification settings

sebastianpoeplau/yaml

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YAML

An updated YAML library for Clojure based on Snake YAML and heavily inspired by clj-yaml

Install

Lein

Clojars Project

Usage

(ns demo.core
  (:refer-clojure :exclude [load])
  (:require [yaml.core :as yaml]))

;; Note on DSL
;; yaml/load & yaml/parse-string are identical
;; yaml/dump & yaml/generate-string are identical

;; Parse a YAML file

(yaml/from-file "config.yml")

;; Parse a YAML string

(yaml/parse-string "foo: bar")

;; Dump YAML

(yaml/generate-string {:foo "bar"})

;; Examples

(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})

;; Output Formatting examples

(def data [{:name "John Smith", :age 33} {:name "Mary Smith", :age 27}])

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

(yaml/generate-string data :dumper-options {:flow-style :flow})
=> [{age: 33, name: John Smith}, {age: 27, name: Mary Smith}]

(yaml/generate-string data :dumper-options {:flow-style :block})
=> - age: 33
     name: John Smith
   - age: 27
     name: Mary Smith

(yaml/generate-string data :dumper-options {:flow-style :flow :scalar-style :single-quoted})
=> [{'age': !!int '33', 'name': 'John Smith'}, {'age': !!int '27', 'name': 'Mary Smith'}]

Valid values for flow-style are:
- :auto
- :block
- :flow

Valid values for scalar-style are:
- :double-quoted
- :single-quoted
- :literal
- :folded
- :plain

All are documented at http://yaml.org/spec/current.html

This is mainly an updated version of clj-yaml with some updates

  1. Updates snake YAML to latest version
  2. Split reader and writer into separate protocols and files
  3. Ability to read YAML from file in single function
  4. Return vector [] instead of list when parsing java.util.ArrayList
  5. Ability to parse multiple documents

License

Copyright © 2016 Owain Lewis

Distributed under the Eclipse Public License

About

A fast, idiomatic and easy to use Clojure YAML library. Based on Snake YAML

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Clojure 100.0%