On Tue, Dec 2, 2008 at 12:52 PM, Rich Hickey <[EMAIL PROTECTED]> wrote: > > On Dec 2, 7:02 am, Parth Malwankar <[EMAIL PROTECTED]> wrote: >> Tayssir John Gabbour wrote: >> > Hi! >> >> > How should I approach serialization? I made a little test function >> > which serializes and deserializes Clojure objects. It works for >> > strings, integers, symbols, LazilyPersistentVectors and.. oddly.. >> > PersistentHashMaps that have exactly one element. (My Clojure is about >> > a month old.) >> >> I am not much of a Java guy so this would be what I would do. >> Clojure has reader syntax of its data structures (maps, vectors etc.) >> so they can be easily written to a stream as test using write. >> >> Reading it is equally simple: >> >> user=> (def a (with-in-str "{:a 1 :b 2}" (read))) >> #'user/a >> user=> a >> {:a 1, :b 2} >> user=> >> >> So as long as your data has reader syntax its not too much >> of an issue. If the data needs to be shared between languages >> as highlighted by someone, you may consider using another >> format like json or so. >> >> If the data used java object it may not be serializable so >> easily. Generally my approach is to stick to data structures >> at Clojure level as it has reader syntax. >> > > Yes, please consider print/read. It is readable text, works with a lot > of data structures, and is extensible. > > As part of AOT I needed to enhance print/read to store constants of > many kinds, and restore faithfully. This led to a new multimethod - > print-dup, for high-fidelity printing. You can get print-dup behavior > by binding *print-dup*: > > (binding [*print-dup* true] > (dorun > (map prn > [[1 2 3] > {4 5 6 7} > (java.util.ArrayList. [8 9]) > String > "string" > 42M > :hello > #"ethel" > (sorted-set 9 8 7 6) > #'rest]))) > > [1 2 3] > {4 5, 6 7} > #=(java.util.ArrayList. [8 9]) > #=java.lang.String > "string" > 42M > :hello > #"ethel" > #=(clojure.lang.PersistentTreeSet/create [6 7 8 9]) > #=(var clojure.core/rest) > > It can handle all of the Clojure data structures (including sorted > variants), Java collections, classes etc. > > You can extend it to new types by defining the print-dup method for > the type.
I don't understand what the print-dup mechanism outputs and how to reconstruct objects from that output later. I was expecting an API similar to this. (def my-string (print-dup [1 2 3])) (def my-data (read my-string)) Can you give a simple example of serializing and deserializing a Clojure collection? -- R. Mark Volkmann Object Computing, Inc. --~--~---------~--~----~------------~-------~--~----~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---