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.
Rich
>
> > But for other things, like keywords and most PersistentHashMaps, it
> > throws NotSerializableException.
>
> > My imagined possible solutions:
>
> > * Implement Serializable for Clojure data -- but is it possible in a
> > dynamic "Hey I'll just write a new method!" way?
>
> > * Go into Clojure's source and implement Serializable to the Java
> > classes.
>
> > My end goal is using a nonrelational DB like Tokyo Cabinet or
> > BerkeleyDB.
>
> > Thanks,
> > Tayssir
>
> > PS: Here's my test code:
>
> > (defn my-identity "Copies obj through serialization and
> > deserialization."
> > [obj]
> > (let [byte-out (new java.io.ByteArrayOutputStream)
> > obj-out (new java.io.ObjectOutputStream byte-out)]
> > (try (.writeObject obj-out obj)
> > (finally (.close obj-out)))
> > (let [obj-in (new java.io.ObjectInputStream
> > (new java.io.ByteArrayInputStream (.toByteArray
> > byte-out)))]
> > (try (.readObject obj-in)
> > (finally (.close obj-in))))))
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---