Oh wow! I shouldn't have turned my computer off yesterday evening!!!
there were many suggestions to try out... :)
Ok let's see...I've got a record with meta-data let's call it FOO and
its instance MFOO:
user=> (defrecord FOO [a b c])
user.FOO
user=> (def MFOO (with-meta (FOO. 1 2 3) {:x false :y true}))
#'user/MFOO
Now I want to serialise it but keep the meta-data when reading it back
in. pint-dup seems like my only option as it preserves metadata. As you
say though, there is no dispatch for '=' so it seems like I'm stuck with
standard java serialisation. Even i I convert it to a map with metadata
the same thing will happen (#=)...
Is there no way to keep the metadata?
many many thanks :)
Jim
On 21/08/13 23:26, kawas wrote:
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
<http://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.
--
--
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.