In fact never user *print-dup* when using edn to read back data

*print-dup* will output type and meta information that will not play well 
with edn
  user=> (binding [*print-dup* true] (pr-str (sorted-map :z 5 :a 1)))
  "#=(clojure.lang.PersistentTreeMap/create {:a 1, :z 5})"

This "#=" with raise a RuntimeException because there is no dispatch on "=" 
in edn

Not using *print-dup* may lose some important information on data
  user=> (binding [*print-dup* false] (pr-str (sorted-map :z 5 :a 1)))
  "{:a 1, :z 5}"  ;; this is a plain hash map

Just take care of providing your own custom tags to be able to read data 
correctly
  ;; clumsy example
  user=> (binding [*print-dup* false] (str "#treemap " (pr-str (sorted-map 
:z 5 :a 1))))
  "#treemap {:a 1, :z 5}"

You can then read it back with your custom reader
  user=> (edn/read-string {:readers {'treemap #(merge (sorted-map) %)}}
                 (binding [*print-dup* false] (str "#treemap " (pr-str 
(sorted-map :z 5 :a 1)))))
  {:a 1, :z 5}   ;; this is a sorted map


Seems like it should be possible to easily provide default printers and 
readers for custom tags & data...

Cheers

Le mercredi 21 août 2013 22:38:18 UTC+2, kawas a écrit :
>
> Never had time to play with edn and custom readers but they are funny :)
>
> Use *print-dup* if you need to... just know how to custom read it :
>
>   user=> (edn/read-string {:readers {'user.Foo map->Foo}} "#user.Foo{:a 1 
> :b 2 :c 3}")
>   #user.Foo{:a 1, :b 2, :c 3}
>
>   user=> (edn/read-string {:readers {'user.Foo #(apply ->Foo %)}} 
> "#user.Foo[1, 2, 3]")
>   #user.Foo{:a 1, :b 2, :c 3}
>
> Nice
>
> Le mercredi 21 août 2013 22:33:00 UTC+2, kawas a écrit :
>>
>> By the way *print-dup* is the problem, maybe you should not use it :)
>>
>> Spot the difference :
>>   user=> (binding [*print-dup* true] (prn (->Foo 1 2 3)))
>>   #user.Foo[1, 2, 3]
>>
>>   user=> (binding [*print-dup* false] (prn (->Foo 1 2 3)))
>>   #user.Foo{:a 1, :b 2, :c 3}
>>
>> cheers
>>
>> Le mercredi 21 août 2013 19:55:59 UTC+2, Jim foo.bar a écrit :
>>>
>>>  Hi everyone,
>>>
>>> I am trying to serialise a record with edn. I think I am using all the 
>>> good practices but I get a weird error...I am using these simple functions:
>>>
>>> (defn data->string
>>> "Writes the object b on a file f on disk as a string."
>>> [b f]
>>> (io!
>>> (with-open [w (clojure.java.io/writer f)]
>>>   (binding [*print-dup* true 
>>>             *out* w]  (prn b)))))
>>>
>>> (defn string->data
>>> "Read the file f back on memory safely. Contents of f should be a 
>>> clojure data-structure." 
>>> [f]
>>> (io!
>>>  (edn/read-string (slurp f))))  
>>>
>>> and I am getting this error:
>>> *
>>> **RuntimeException No dispatch macro for: =  
>>> clojure.lang.Util.runtimeException (Util.java:219)*
>>>
>>> Out of curiosity I tried something simpler like this: 
>>>
>>> Clondie24.games.chess=> (defrecord FOO [a b c])
>>> Clondie24.games.chess.FOO
>>> Clondie24.games.chess=> (ut/data->string (FOO. :a :b :C) "FOO")
>>> nil
>>> Clondie24.games.chess=> (ut/string->data "FOO")
>>>
>>> RuntimeException No reader function for tag Clondie24.games.chess.FOO  
>>> clojure.lang.EdnReader$TaggedReader.readTagged (EdnReader.java:739)
>>>
>>>
>>> What I am I missing? any suggestions? 
>>>
>>> Jim
>>>                           
>>>  
>>>  
>>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to