├── .gitignore ├── project.clj ├── LICENSE ├── README.md ├── src └── clj_yaml │ └── core.clj ├── test └── clj_yaml │ └── core_test.clj └── epl-v10.html /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | classes 3 | *.jar 4 | .* 5 | pom.xml 6 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clj-yaml "0.4.0" 2 | :description "YAML encoding and decoding for Clojure using SnakeYAML" 3 | :url "http://github.com/lancepantz/clj-yaml" 4 | :dependencies 5 | [[org.clojure/clojure "1.2.0"] 6 | [org.yaml/snakeyaml "1.5"]]) 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (c) Lance Bradley - Licensed under the same terms as clojure itself. 2 | 3 | The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0) which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by terms of this license. You must not remove this notice, or any other, from this software. 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deprecation 2 | This library is no longer maintained. An [actively developed fork](https://github.com/clj-commons/clj-yaml) is suggested. 3 | 4 | # Historical README 5 | 6 | `clj-yaml` provides [YAML](http://yaml.org) encoding and decoding for Clojure via the [SnakeYAML](http://code.google.com/p/snakeyaml/) Java library. 7 | 8 | ## Usage 9 | 10 | (require '[clj-yaml.core :as yaml]) 11 | 12 | (yaml/generate-string 13 | [{:name "John Smith", :age 33} 14 | {:name "Mary Smith", :age 27}]) 15 | "- {name: John Smith, age: 33}\n- {name: Mary Smith, age: 27}\n" 16 | 17 | (yaml/parse-string " 18 | - {name: John Smith, age: 33} 19 | - name: Mary Smith 20 | age: 27 21 | ") 22 | => ({:name "John Smith", :age 33} 23 | {:name "Mary Smith", :age 27}) 24 | 25 | ## Installation 26 | 27 | `clj-yaml` is available as a Maven artifact from [Clojars](http://clojars.org/clj-yaml): 28 | 29 | :dependencies 30 | [["clj-yaml" "0.4.0"] 31 | ...] 32 | 33 | ## Development 34 | 35 | $ git clone git://github.com/lancepantz/clj-yaml.git 36 | $ lein deps 37 | $ lein test 38 | $ lein install 39 | -------------------------------------------------------------------------------- /src/clj_yaml/core.clj: -------------------------------------------------------------------------------- 1 | (ns clj-yaml.core 2 | (:import (org.yaml.snakeyaml Yaml DumperOptions DumperOptions$FlowStyle))) 3 | 4 | (def ^{:dynamic true} *keywordize* true) 5 | 6 | (def flow-styles 7 | {:auto DumperOptions$FlowStyle/AUTO 8 | :block DumperOptions$FlowStyle/BLOCK 9 | :flow DumperOptions$FlowStyle/FLOW}) 10 | 11 | (defn make-dumper-options 12 | [& {:keys [flow-style]}] 13 | (doto (DumperOptions.) 14 | (.setDefaultFlowStyle (flow-styles flow-style)))) 15 | 16 | (defn make-yaml 17 | [& {:keys [dumper-options]}] 18 | (if dumper-options 19 | (Yaml. (apply make-dumper-options 20 | (mapcat (juxt key val) 21 | dumper-options))) 22 | (Yaml.))) 23 | 24 | (defprotocol YAMLCodec 25 | (encode [data]) 26 | (decode [data])) 27 | 28 | (defn decode-key [k] 29 | (if *keywordize* (keyword k) k)) 30 | 31 | (extend-protocol YAMLCodec 32 | 33 | clojure.lang.IPersistentMap 34 | (encode [data] 35 | (into {} 36 | (for [[k v] data] 37 | [(encode k) (encode v)]))) 38 | 39 | clojure.lang.IPersistentCollection 40 | (encode [data] 41 | (map encode data)) 42 | 43 | clojure.lang.Keyword 44 | (encode [data] 45 | (name data)) 46 | 47 | java.util.LinkedHashMap 48 | (decode [data] 49 | (into {} 50 | (for [[k v] data] 51 | [(decode-key k) (decode v)]))) 52 | 53 | java.util.LinkedHashSet 54 | (decode [data] 55 | (into #{} data)) 56 | 57 | java.util.ArrayList 58 | (decode [data] 59 | (map decode data)) 60 | 61 | Object 62 | (encode [data] data) 63 | (decode [data] data) 64 | 65 | nil 66 | (encode [data] data) 67 | (decode [data] data)) 68 | 69 | (defn generate-string [data & opts] 70 | (.dump (apply make-yaml opts) 71 | (encode data))) 72 | 73 | (defn parse-string 74 | ([string keywordize] 75 | (binding [*keywordize* keywordize] 76 | (parse-string string))) 77 | ([string] 78 | (decode (.load (make-yaml) string)))) 79 | -------------------------------------------------------------------------------- /test/clj_yaml/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns clj-yaml.core-test 2 | (:use clojure.test) 3 | (:use clj-yaml.core)) 4 | 5 | (def nested-hash-yaml 6 | "root:\n childa: a\n childb: \n grandchild: \n greatgrandchild: bar\n") 7 | 8 | (def list-yaml 9 | "--- # Favorite Movies\n- Casablanca\n- North by Northwest\n- The Man Who Wasn't There") 10 | 11 | (def hashes-lists-yaml " 12 | items: 13 | - part_no: A4786 14 | descrip: Water Bucket (Filled) 15 | price: 1.47 16 | quantity: 4 17 | 18 | - part_no: E1628 19 | descrip: High Heeled \"Ruby\" Slippers 20 | price: 100.27 21 | quantity: 1 22 | owners: 23 | - Dorthy 24 | - Wicked Witch of the East 25 | ") 26 | 27 | (def inline-list-yaml " 28 | --- # Shopping list 29 | [milk, pumpkin pie, eggs, juice] 30 | ") 31 | 32 | (def inline-hash-yaml 33 | "{name: John Smith, age: 33}") 34 | 35 | (def list-of-hashes-yaml " 36 | - {name: John Smith, age: 33} 37 | - name: Mary Smith 38 | age: 27 39 | ") 40 | 41 | (def hashes-of-lists-yaml " 42 | men: [John Smith, Bill Jones] 43 | women: 44 | - Mary Smith 45 | - Susan Williams 46 | ") 47 | 48 | (def typed-data-yaml " 49 | the-bin: !!binary 0101") 50 | 51 | (def set-yaml " 52 | --- !!set 53 | ? Mark McGwire 54 | ? Sammy Sosa 55 | ? Ken Griff") 56 | 57 | (deftest parse-hash 58 | (let [parsed (parse-string "foo: bar")] 59 | (is (= "bar" (parsed :foo))))) 60 | 61 | (deftest parse-nested-hash 62 | (let [parsed (parse-string nested-hash-yaml)] 63 | (is (= "a" ((parsed :root) :childa))) 64 | (is (= "bar" ((((parsed :root) :childb) :grandchild) :greatgrandchild))))) 65 | 66 | (deftest parse-list 67 | (let [parsed (parse-string list-yaml)] 68 | (is (= "Casablanca" (first parsed))) 69 | (is (= "North by Northwest" (nth parsed 1))) 70 | (is (= "The Man Who Wasn't There" (nth parsed 2))))) 71 | 72 | (deftest parse-nested-hash-and-list 73 | (let [parsed (parse-string hashes-lists-yaml)] 74 | (is (= "A4786" ((first (parsed :items)) :part_no))) 75 | (is (= "Dorthy" (first ((nth (parsed :items) 1) :owners)))))) 76 | 77 | (deftest parse-inline-list 78 | (let [parsed (parse-string inline-list-yaml)] 79 | (is (= "milk" (first parsed))) 80 | (is (= "pumpkin pie" (nth parsed 1))) 81 | (is (= "eggs" (nth parsed 2))) 82 | (is (= "juice" (last parsed))))) 83 | 84 | (deftest parse-inline-hash 85 | (let [parsed (parse-string inline-hash-yaml)] 86 | (is (= "John Smith" (parsed :name))) 87 | (is (= 33 (parsed :age))))) 88 | 89 | (deftest parse-list-of-hashes 90 | (let [parsed (parse-string list-of-hashes-yaml)] 91 | (is (= "John Smith" ((first parsed) :name))) 92 | (is (= 33 ((first parsed) :age))) 93 | (is (= "Mary Smith" ((nth parsed 1) :name))) 94 | (is (= 27 ((nth parsed 1) :age))))) 95 | 96 | (deftest hashes-of-lists 97 | (let [parsed (parse-string hashes-of-lists-yaml)] 98 | (is (= "John Smith" (first (parsed :men)))) 99 | (is (= "Bill Jones" (last (parsed :men)))) 100 | (is (= "Mary Smith" (first (parsed :women)))) 101 | (is (= "Susan Williams" (last (parsed :women)))))) 102 | 103 | (deftest h-set 104 | (is (= #{"Mark McGwire" "Ken Griff" "Sammy Sosa"} 105 | (parse-string set-yaml)))) 106 | 107 | (deftest typed-data 108 | (let [parsed (parse-string typed-data-yaml)] 109 | (is (= (Class/forName "[B") (type (:the-bin parsed)))))) 110 | 111 | (deftest keywordized 112 | (binding [*keywordize* false] 113 | (is (= "items" (-> hashes-lists-yaml parse-string ffirst)))) 114 | (is (= "items" (-> hashes-lists-yaml (parse-string false) ffirst)))) 115 | 116 | (deftest dump-opts 117 | (let [data [{:age 33 :name "jon"} {:age 44 :name "boo"}]] 118 | (is (= "- age: 33\n name: jon\n- age: 44\n name: boo\n" 119 | (generate-string data :dumper-options {:flow-style :block}))) 120 | (is (= "[{age: 33, name: jon}, {age: 44, name: boo}]\n" 121 | (generate-string data :dumper-options {:flow-style :flow}))))) -------------------------------------------------------------------------------- /epl-v10.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Eclipse Public License - Version 1.0 8 | 25 | 26 | 27 | 28 | 29 | 30 |

Eclipse Public License - v 1.0

31 | 32 |

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 33 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR 34 | DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS 35 | AGREEMENT.

36 | 37 |

1. DEFINITIONS

38 | 39 |

"Contribution" means:

40 | 41 |

a) in the case of the initial Contributor, the initial 42 | code and documentation distributed under this Agreement, and

43 |

b) in the case of each subsequent Contributor:

44 |

i) changes to the Program, and

45 |

ii) additions to the Program;

46 |

where such changes and/or additions to the Program 47 | originate from and are distributed by that particular Contributor. A 48 | Contribution 'originates' from a Contributor if it was added to the 49 | Program by such Contributor itself or anyone acting on such 50 | Contributor's behalf. Contributions do not include additions to the 51 | Program which: (i) are separate modules of software distributed in 52 | conjunction with the Program under their own license agreement, and (ii) 53 | are not derivative works of the Program.

54 | 55 |

"Contributor" means any person or entity that distributes 56 | the Program.

57 | 58 |

"Licensed Patents" mean patent claims licensable by a 59 | Contributor which are necessarily infringed by the use or sale of its 60 | Contribution alone or when combined with the Program.

61 | 62 |

"Program" means the Contributions distributed in accordance 63 | with this Agreement.

64 | 65 |

"Recipient" means anyone who receives the Program under 66 | this Agreement, including all Contributors.

67 | 68 |

2. GRANT OF RIGHTS

69 | 70 |

a) Subject to the terms of this Agreement, each 71 | Contributor hereby grants Recipient a non-exclusive, worldwide, 72 | royalty-free copyright license to reproduce, prepare derivative works 73 | of, publicly display, publicly perform, distribute and sublicense the 74 | Contribution of such Contributor, if any, and such derivative works, in 75 | source code and object code form.

76 | 77 |

b) Subject to the terms of this Agreement, each 78 | Contributor hereby grants Recipient a non-exclusive, worldwide, 79 | royalty-free patent license under Licensed Patents to make, use, sell, 80 | offer to sell, import and otherwise transfer the Contribution of such 81 | Contributor, if any, in source code and object code form. This patent 82 | license shall apply to the combination of the Contribution and the 83 | Program if, at the time the Contribution is added by the Contributor, 84 | such addition of the Contribution causes such combination to be covered 85 | by the Licensed Patents. The patent license shall not apply to any other 86 | combinations which include the Contribution. No hardware per se is 87 | licensed hereunder.

88 | 89 |

c) Recipient understands that although each Contributor 90 | grants the licenses to its Contributions set forth herein, no assurances 91 | are provided by any Contributor that the Program does not infringe the 92 | patent or other intellectual property rights of any other entity. Each 93 | Contributor disclaims any liability to Recipient for claims brought by 94 | any other entity based on infringement of intellectual property rights 95 | or otherwise. As a condition to exercising the rights and licenses 96 | granted hereunder, each Recipient hereby assumes sole responsibility to 97 | secure any other intellectual property rights needed, if any. For 98 | example, if a third party patent license is required to allow Recipient 99 | to distribute the Program, it is Recipient's responsibility to acquire 100 | that license before distributing the Program.

101 | 102 |

d) Each Contributor represents that to its knowledge it 103 | has sufficient copyright rights in its Contribution, if any, to grant 104 | the copyright license set forth in this Agreement.

105 | 106 |

3. REQUIREMENTS

107 | 108 |

A Contributor may choose to distribute the Program in object code 109 | form under its own license agreement, provided that:

110 | 111 |

a) it complies with the terms and conditions of this 112 | Agreement; and

113 | 114 |

b) its license agreement:

115 | 116 |

i) effectively disclaims on behalf of all Contributors 117 | all warranties and conditions, express and implied, including warranties 118 | or conditions of title and non-infringement, and implied warranties or 119 | conditions of merchantability and fitness for a particular purpose;

120 | 121 |

ii) effectively excludes on behalf of all Contributors 122 | all liability for damages, including direct, indirect, special, 123 | incidental and consequential damages, such as lost profits;

124 | 125 |

iii) states that any provisions which differ from this 126 | Agreement are offered by that Contributor alone and not by any other 127 | party; and

128 | 129 |

iv) states that source code for the Program is available 130 | from such Contributor, and informs licensees how to obtain it in a 131 | reasonable manner on or through a medium customarily used for software 132 | exchange.

133 | 134 |

When the Program is made available in source code form:

135 | 136 |

a) it must be made available under this Agreement; and

137 | 138 |

b) a copy of this Agreement must be included with each 139 | copy of the Program.

140 | 141 |

Contributors may not remove or alter any copyright notices contained 142 | within the Program.

143 | 144 |

Each Contributor must identify itself as the originator of its 145 | Contribution, if any, in a manner that reasonably allows subsequent 146 | Recipients to identify the originator of the Contribution.

147 | 148 |

4. COMMERCIAL DISTRIBUTION

149 | 150 |

Commercial distributors of software may accept certain 151 | responsibilities with respect to end users, business partners and the 152 | like. While this license is intended to facilitate the commercial use of 153 | the Program, the Contributor who includes the Program in a commercial 154 | product offering should do so in a manner which does not create 155 | potential liability for other Contributors. Therefore, if a Contributor 156 | includes the Program in a commercial product offering, such Contributor 157 | ("Commercial Contributor") hereby agrees to defend and 158 | indemnify every other Contributor ("Indemnified Contributor") 159 | against any losses, damages and costs (collectively "Losses") 160 | arising from claims, lawsuits and other legal actions brought by a third 161 | party against the Indemnified Contributor to the extent caused by the 162 | acts or omissions of such Commercial Contributor in connection with its 163 | distribution of the Program in a commercial product offering. The 164 | obligations in this section do not apply to any claims or Losses 165 | relating to any actual or alleged intellectual property infringement. In 166 | order to qualify, an Indemnified Contributor must: a) promptly notify 167 | the Commercial Contributor in writing of such claim, and b) allow the 168 | Commercial Contributor to control, and cooperate with the Commercial 169 | Contributor in, the defense and any related settlement negotiations. The 170 | Indemnified Contributor may participate in any such claim at its own 171 | expense.

172 | 173 |

For example, a Contributor might include the Program in a commercial 174 | product offering, Product X. That Contributor is then a Commercial 175 | Contributor. If that Commercial Contributor then makes performance 176 | claims, or offers warranties related to Product X, those performance 177 | claims and warranties are such Commercial Contributor's responsibility 178 | alone. Under this section, the Commercial Contributor would have to 179 | defend claims against the other Contributors related to those 180 | performance claims and warranties, and if a court requires any other 181 | Contributor to pay any damages as a result, the Commercial Contributor 182 | must pay those damages.

183 | 184 |

5. NO WARRANTY

185 | 186 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 187 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 188 | OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 189 | ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 190 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 191 | responsible for determining the appropriateness of using and 192 | distributing the Program and assumes all risks associated with its 193 | exercise of rights under this Agreement , including but not limited to 194 | the risks and costs of program errors, compliance with applicable laws, 195 | damage to or loss of data, programs or equipment, and unavailability or 196 | interruption of operations.

197 | 198 |

6. DISCLAIMER OF LIABILITY

199 | 200 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT 201 | NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 202 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 203 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 204 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 205 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 206 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 207 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

208 | 209 |

7. GENERAL

210 | 211 |

If any provision of this Agreement is invalid or unenforceable under 212 | applicable law, it shall not affect the validity or enforceability of 213 | the remainder of the terms of this Agreement, and without further action 214 | by the parties hereto, such provision shall be reformed to the minimum 215 | extent necessary to make such provision valid and enforceable.

216 | 217 |

If Recipient institutes patent litigation against any entity 218 | (including a cross-claim or counterclaim in a lawsuit) alleging that the 219 | Program itself (excluding combinations of the Program with other 220 | software or hardware) infringes such Recipient's patent(s), then such 221 | Recipient's rights granted under Section 2(b) shall terminate as of the 222 | date such litigation is filed.

223 | 224 |

All Recipient's rights under this Agreement shall terminate if it 225 | fails to comply with any of the material terms or conditions of this 226 | Agreement and does not cure such failure in a reasonable period of time 227 | after becoming aware of such noncompliance. If all Recipient's rights 228 | under this Agreement terminate, Recipient agrees to cease use and 229 | distribution of the Program as soon as reasonably practicable. However, 230 | Recipient's obligations under this Agreement and any licenses granted by 231 | Recipient relating to the Program shall continue and survive.

232 | 233 |

Everyone is permitted to copy and distribute copies of this 234 | Agreement, but in order to avoid inconsistency the Agreement is 235 | copyrighted and may only be modified in the following manner. The 236 | Agreement Steward reserves the right to publish new versions (including 237 | revisions) of this Agreement from time to time. No one other than the 238 | Agreement Steward has the right to modify this Agreement. The Eclipse 239 | Foundation is the initial Agreement Steward. The Eclipse Foundation may 240 | assign the responsibility to serve as the Agreement Steward to a 241 | suitable separate entity. Each new version of the Agreement will be 242 | given a distinguishing version number. The Program (including 243 | Contributions) may always be distributed subject to the version of the 244 | Agreement under which it was received. In addition, after a new version 245 | of the Agreement is published, Contributor may elect to distribute the 246 | Program (including its Contributions) under the new version. Except as 247 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives no 248 | rights or licenses to the intellectual property of any Contributor under 249 | this Agreement, whether expressly, by implication, estoppel or 250 | otherwise. All rights in the Program not expressly granted under this 251 | Agreement are reserved.

252 | 253 |

This Agreement is governed by the laws of the State of New York and 254 | the intellectual property laws of the United States of America. No party 255 | to this Agreement will bring a legal action under this Agreement more 256 | than one year after the cause of action arose. Each party waives its 257 | rights to a jury trial in any resulting litigation.

258 | 259 | 260 | 261 | 262 | --------------------------------------------------------------------------------