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.

Parth


> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to