Serializing clojure data structures

7 views
Skip to first unread message

Serge Wroclawski

unread,
Apr 20, 2009, 11:10:07 AM4/20/09
to Clojure Study Group Washington DC
In Python I can use pickle to serialize a data structure.

What's the Clojure way to do that?

Print to a file isn't good enough if I'm using Java time objects.

- Serge

Keith Bennett

unread,
Apr 20, 2009, 2:01:36 PM4/20/09
to clojure-...@googlegroups.com
Serge -

Java has its own serialization mechanism. Assuming all the Clojure
things reduce down to Java objects, we could theoretically use that.
The benefit is that we would not need to concern ourselves with
implementing the mechanism. The down side is that is not very
readable, and would be unintelligible to non-Java consumers.

That said, I'm sure Rich Hickey and the Clojure gurus would have
something useful to say about the matter.

- Keith

Serge Wroclawski

unread,
Apr 20, 2009, 2:11:19 PM4/20/09
to clojure-...@googlegroups.com
On Mon, Apr 20, 2009 at 2:01 PM, Keith Bennett <keithr...@gmail.com> wrote:
>
> Serge -
>
> Java has its own serialization mechanism.  Assuming all the Clojure
> things reduce down to Java objects, we could theoretically use that.
> The benefit is that we would not need to concern ourselves with
> implementing the mechanism.  The down side is that is not very
> readable, and would be unintelligible to non-Java consumers.

The file format is unreadable? That's totally fine.

If the library and usage are complex and unreadable, that's less fine. :)

I just want a way to capture data and serialize the data to disk periodically.

What's it called?

- Serge

Michael Fogus

unread,
Apr 20, 2009, 3:06:06 PM4/20/09
to clojure-...@googlegroups.com
I've been using the JSON reader/writer in my own projects and have
found it simple and adequate. However, I am not sure what the
implications of trying to embed Java time objects. My guess is that
the toString of the Time object will not be in a JSON-legal format for
deserialization. And even if it was, then there would be no way to
put it back into a Time instance. However, with minimal hacking you
could probably make it do so.

The pretty printing functionality is now included in clojure-contrib,
but I have not had a chance to play with it. I doubt it will give you
what you're looking for, but I can't say for sure. It might be worth
exploring.
-m

Keith Bennett

unread,
Apr 20, 2009, 3:25:32 PM4/20/09
to clojure-...@googlegroups.com
Serge -

Here's a sample Clojure program that illustrates serializing out to a
file; reading it back in works the same, but using the input stream
objects instead of the output stream objects below:

----

(ns stream-example
(:import (java.io ObjectOutputStream FileOutputStream)))

(def fos (FileOutputStream. "foo.tmp"))
(def oos (ObjectOutputStream. fos))

(doto oos
(.writeInt 12345)
(.writeObject "today")
(.writeObject (java.util.Date.))
(.close))

(println "Examine foo.tmp to see the output. Using od -x might be good...")

----

Here are some resources for info about Java serialization:

http://www.deitel.com/articles/java_tutorials/20050923/IntroductionToObjectSerialization.html
http://java.sun.com/developer/technicalArticles/Programming/serialization/

In order for a Java object to be serializable, it must be declared to
implement the interface java.io.Serializable. I would imagine that
Clojure objects are, but am not sure.

- Keith

Keith Bennett

unread,
Apr 20, 2009, 3:58:18 PM4/20/09
to clojure-...@googlegroups.com
I suppose it would be possible to attach uuencode/uudecode filters or
the like to the binary data streams so that they could be stored in
text (and JSON) files. However, remember that Java serializes an
entire object graph, and is not what we are used to seeing in JSON
data. So it can get tricky, making sure that a huge graph is pulled
in because of the composition of the objects involved.

- Keith

Keith Bennett

unread,
Apr 20, 2009, 4:05:29 PM4/20/09
to clojure-...@googlegroups.com
Sorry, that should have been "...making sure that a huge graph is NOT
pulled in...".

- Keith
Reply all
Reply to author
Forward
0 new messages