Clojure is designed with enough extensibility to modify the printer and 
reader to cover this case.

You can define a custom print strategy for these types by extending the 
print-dup multimethod. If you print it as a tagged literal, you can also 
define a custom reader that can read it back as the appropriate type. Or 
you can just happen to write it as a form that constructs a Java object if 
that's possible. (I don't know the Joda classes well.)

Print methods typically look like this:

(defmethod print-dup org.joda.time.DateTime
  [date-time ^java.io.Writer w]
  (.write w (format "#org.joda.time.DateTime[%d]")))

That particular format should produce a string like 
#org.joda.time.DateTime[1462809085214] which when read will call the 
DateTime constructor with the long value. 

Or you could write your own custom tagged literal format and define and 
install a reader for that tag that refers to a function that does whatever 
you need.

And I would recommend never using Java serialization in either Clojure or 
Java. :) 

On Monday, May 9, 2016 at 9:44:36 AM UTC-5, Simon Brooke wrote:
>
> I'm working on some complex analysis where it takes about twelve hours to 
> construct the model to be analysed. Once it's constructed I'm doing various 
> interactive things to explore it, but I can't currently persist it in a 
> form in which it can be read back in, so that when the REPL session ends it 
> is lost and I need to run the construction process again.
>
> When I was creating the system I had thought, airily, 'well, Clojure is 
> Lisp so all I need to do is spit the structure out to a file and slurp it 
> back in again'. Unfortunately, of course, Clojure is only mostly Lisp.
>
> My problem is clj-time objects, both date-time and interval. These are 
> essentially Joda time objects, and when they are printed, what you get is a 
> string representation of an object reference. When the reader reads it 
> back, what you have is a string:
>
> user=> (def foo (tc/now))
>
> #'user/foo
>
> user=> (spit "/tmp/saved-time" (with-out-str (pr foo)))
>
> nil
>
> user=> (def bar (slurp "/tmp/saved-time"))
>
> #'user/bar
>
> user=> (= foo bar)
>
> false
>
> user=> (type foo)
>
> org.joda.time.DateTime
>
> user=> (type bar)
>
> java.lang.String
>
>
> So, my question: in this particular case, what is the simplest means of 
> writing my data structures (which, apart from the clj-time objects, 
> comprises deeply nested maps of keywords, strings and numbers - all things 
> which the Clojure reader and writer can cope with) to disk in a form in 
> which they can subsequently be read back?
>
>
> There are two obvious solutions: one is to have a function which walks the 
> data structure and returns an equivalent data structure in which each 
> clj-time object is replaced by a function invocation which when invoked 
> creates an equivalent clj-time object; the other is to use Java 
> serialisation to serialise the whole structure as in the answer to this 
> stackoverflow question 
> <http://stackoverflow.com/questions/7701634/efficient-binary-serialization-for-clojure-java>
> .
>
>
> Which of these would people go for? Or is there some other solution I have 
> not thought of?
>

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to