Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can deserialize a Set, but can't serialize a Set #11

Open
trevor opened this issue Jun 2, 2013 · 2 comments
Open

can deserialize a Set, but can't serialize a Set #11

trevor opened this issue Jun 2, 2013 · 2 comments

Comments

@trevor
Copy link

trevor commented Jun 2, 2013

;; works:

(def typed-data-yaml "
the-bin: !!binary 0101")

(clj-yaml.core/parse-string typed-data-yaml)
;= {:the-bin #<byte[] [B@61911e64>}

(clj-yaml.core/generate-string
  (clj-yaml.core/parse-string typed-data-yaml)
  :dumper-options {:flow-style :block})
;= "the-bin: !!binary |-\n  0101\n"


;; generate-string creates a sequence instead of a set:

(def set-yaml "
--- !!set
? Mark McGwire
? Sammy Sosa
? Ken Griff")

(clj-yaml.core/parse-string set-yaml)
;= #{"Mark McGwire" "Ken Griff" "Sammy Sosa"}

(clj-yaml.core/generate-string
  (clj-yaml.core/parse-string set-yaml)
  :dumper-options {:flow-style :block})
;= "- Mark McGwire\n- Ken Griff\n- Sammy Sosa\n"

(clj-yaml.core/parse-string
  (clj-yaml.core/generate-string
    (clj-yaml.core/parse-string set-yaml)
    :dumper-options {:flow-style :block}))
;= ("Mark McGwire" "Ken Griff" "Sammy Sosa")
@trevor
Copy link
Author

trevor commented Jun 3, 2013

This allows for Sets that contain numbers only, or strings only:

  clojure.lang.PersistentHashSet
  (encode [data]
    (java.util.HashSet. data))

  clojure.lang.PersistentTreeSet
  (encode [data]
    (java.util.TreeSet. data))

@trevor
Copy link
Author

trevor commented Jun 3, 2013

… but this is better:

clojure.lang.IPersistentSet
(encode [data]
  (into #{}
    (map encode data)))

example (with modified fn's):

=> (println (yaml-dump ["y" #{"d" 'b/c :a 2 [3 4 {5 6 :x [7 #{}]}]} "m" nil "n"]))
---
- y
- !!set
  a: null
  2: null
  ? - 3
    - 4
    - 5: 6
      x:
      - 7
      - !!set {}
  : null
  d: null
  ? !!clojure.lang.Symbol
    name: c
    namespace: b
  : null
- m
- null
- n
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant