Created
December 10, 2013 11:13
-
-
Save edbond/7889075 to your computer and use it in GitHub Desktop.
snakeyaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns snakeyaml-example.core | |
(:require [clojure.string :refer (split)]) | |
(:import [org.yaml.snakeyaml.constructor SafeConstructor | |
AbstractConstruct Construct] | |
[org.yaml.snakeyaml.nodes Tag Node ScalarNode NodeId] | |
[snakeyaml-example.core Dice ConstructDice DiceConstructor])) | |
(set! *warn-on-reflection* true) | |
(gen-class | |
:name snakeyaml-example.core.Dice | |
:prefix dice- | |
:state state | |
:init init | |
:constructors {[Integer Integer] []}) | |
(gen-class | |
:name snakeyaml-example.core.ConstructDice | |
:prefix construct-dice- | |
:extends org.yaml.snakeyaml.constructor.AbstractConstruct) | |
(gen-class | |
:name snakeyaml-example.core.DiceConstructor | |
:prefix dice-constructor- | |
:post-init initialize | |
:extends org.yaml.snakeyaml.constructor.SafeConstructor) | |
(defn dice-init | |
[a b] | |
[[] {:a a :b b}]) | |
(defn construct-dice-construct | |
[^ScalarNode node] | |
(let [^String val (.constructScalar node) | |
[as bs] (split val #"d" 2) | |
a (Integer/parseInt as) | |
b (Integer/parseInt bs)] | |
(Dice. a b))) | |
(defn dice-constructor-initialize | |
[this] | |
;; (println "Initialize DiceConstructor!") | |
;; (throw (RuntimeException. "OK!")) | |
(let [yc (.yamlConstructors this)] | |
(.put yc (Tag. "!dice") (ConstructDice.)))) | |
(defn -main [& args] | |
(Dice. (int 3) (int 5)) | |
(DiceConstructor.) | |
(ConstructDice.)) | |
;; class DiceConstructor extends SafeConstructor { | |
;; public DiceConstructor() { | |
;; this.yamlConstructors.put(new Tag("!dice"), new ConstructDice()); | |
;; } | |
;; private class ConstructDice extends AbstractConstruct { | |
;; public Object construct(Node node) { | |
;; String val = (String) constructScalar((ScalarNode) node); | |
;; int position = val.indexOf('d'); | |
;; Integer a = new Integer(val.substring(0, position)); | |
;; Integer b = new Integer(val.substring(position + 1)); | |
;; return new Dice(a, b); | |
;; } | |
;; } | |
;; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject snakeyaml-example "0.1.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:url "http://example.com/FIXME" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.5.1"] | |
[org.yaml/snakeyaml "1.13"]] | |
:main snakeyaml-example.core | |
:aot [snakeyaml-example.core]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment