The following utility functions have worked in many cases for me:
(defn object->file [obj file]
(with-open [outp (java.io.ObjectOutputStream.
(java.io.FileOutputStream. file))]
(.writeObject outp obj)))
(defn file->object [file]
(with-open [inp (java.io.ObjectInputStream. (java.io.FileInputStream. file))]
(.readObject inp)))
(defn freeze
([obj]
(with-open [baos (java.io.ByteArrayOutputStream. 1024)
oos (java.io.ObjectOutputStream. baos)]
(.writeObject oos obj)
(.toByteArray baos)))
([obj & objs]
(freeze (vec (cons obj objs)))))
One caveat though is that currently some of the clojure data types
(like Symbols) that I thought would have been serializable are not. I
think that in the case of Clojure symbols it is being addressed
though.
Hope this helps,
Regards,
Kyle Burton
Sorry, forgot to offer up the inverse of freeze, thaw:
(defn thaw [bytes]
(with-open [bais (java.io.ByteArrayInputStream. bytes)
ois (java.io.ObjectInputStream. bais)]
(.readObject ois)))
Regards,
Kyle
It will work with anything that implements the Serializable interface
in Java. Arrays do implement that interface, as do all the
primitives. With respect to cycles, I'd suspect it does, but would
test it. If you have a repl handy it should be pretty easy to test
those functions out on your data structures.
What class has the cycle? Is it a standard collection?
Regards,
Kyle
--
------------------------------------------------------------------------------
kyle....@gmail.com http://asymmetrical-view.com/
------------------------------------------------------------------------------
I don't know JVM too well, but I think no efficient user-levelsolution is possible. Why? To take care of substructure sharing, you
need to remember a set of shareable values that have already been
serialized, and do "reference equality" comparisons when new new
substructures are serialized.
This comparison and a set implementation can easily be done with
pointers (because you have "<"), but there are no pointers in the JVM,
and no "reference inequality", so you must use linear seeks, making
the time complexity of serialization quadratic, where in C/C++ it
could be O(N log N)
On Mon, Aug 10, 2009 at 10:42 PM, Kyle R. Burton<kyle....@gmail.com> wrote:Sorry, forgot to offer up the inverse of freeze, thaw:
(defn thaw [bytes]
(with-open [bais (java.io.ByteArrayInputStream. bytes)
ois (java.io.ObjectInputStream. bais)]
(.readObject ois)))
Regards,
Kyle