I use YAML to serialize. I needed a simple way to pass Maps, Lists and
Vector between java,  Ruby and Clojure components.
I change the classes of Clojure in the YAML output to java.util.Map,
List and so on to remove dependencies
on Clojure classes while retaining the ability to walk through the
structures using these basic types in "foreign"
components. In Java it's pretty obvious, Map, List and Vectors are
always present and in Ruby these things are
also part of the core language.

Have a look at http://jyaml.sourceforge.net/

Essentially it sums up to something like this:

(def *YAML-config* (YamlConfig.))
(. *YAML-config* load yamlmsg) ;; Loads a YAML representation to an
equivalent object representation
(. *YAML-config* dump  msg) ;; Dumps an object to a YAML string.

I extended a bit the library to deal transparently with types like
java.sql.Date (I deal with several databases)
but nothing else was changed. Just beware of binary characters in your
strings. I encoded these with XML/HTML escapes before serializing.
I need to talk to the maintainer about this issue.

Never liked Java serialization mainly because:

a) The the binary representation of classes has to be exactly the same
at both ends otherwise you are stuck in a dead end.

b) You need that [EMAIL PROTECTED]@[EMAIL PROTECTED] Serializable interface 
which should be
implemented by default everywhere by Java, not you.
    An embedded object misses the interface ? Well find it.. at run-time
and good luck. 

c) It's not easy to debug since it's not human readable.

d) It makes upgrading a distributed environment a pain in the ass since
you may have upgrade everything even if no major
    changes occurred in your classes. You added a method irrelevant to
most of the components in a class ?
    That single change forces you to upgrade everything... this is a
typical example of developpers disconnected from real life.
    In real life your systems are running and you may not be able to
interrupt services for a long period to upgrade them
    all at once. You may have to do so in multiple steps and without
interrupting the service.

e) I want the data serialized, not the access to it...

If size of the YAML output becomes an issue then zip it.

Luc


On Tue, 2008-12-02 at 00:57 -0800, 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.)
> 
> 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