What would be the proper way to serialize this Java object in Clojure?

I have a dummy Java program that I want to write in Clojure. It has a class that implements Serializable and a function that saves it. Since I never wrote such programs in Clojure, I wanted to know what would be the right way to approach this problem, which Clojure data structures and api calls would you use?

import java. io. *; public class Box implements Serializable { private int width; private int height; public void setWidth(int w) { width =w;} public void setHeight(int h) {height = h;} } public static void main (String[] args) { Box myBox =new Box(); myBox.setWidth(50); myBox.setHeight(20) ; try { FileoutputStream fs = new File("foo.ser"); ObjectOUtputStream os = new ObjectOutputStream(fs); os.writeObject(myBox); os . close () ; 

} catch (Exception ex) {}}

+6
java serialization clojure
source share
1 answer

If you want to do this in pure Clojure, use the Clojure reader.

  (use 'clojure.contrib.duck-streams)

 (defn serialize [o filename]
   (binding [* print-dup * true]
    (with-out-writer filename
      (prn o))))

 (defn deserialize [filename]
   (read-string (slurp * filename))) 

Example:

 user> (def box {:height 50 :width 20}) #'user/box user> (serialize box "foo.ser") nil user> (deserialize "foo.ser") {:height 50, :width 20} 

This works for most Clojure objects, but not for most Java objects.

 user> (serialize (java.util.Date.) "date.ser") ; Evaluation aborted. No method in multimethod 'print-dup' for dispatch value: class java.util.Date 

But you can add methods to print-dup multimethod to allow Clojure to print other objects readily.

 user> (defmethod clojure.core/print-dup java.util.Date [ow] (.write w (str "#=(java.util.Date. " (.getTime o) ")"))) #<MultiFn clojure.lang.MultiFn@1af9e98 > user> (serialize (java.util.Date.) "date.ser") nil user> (deserialize "date.ser") #<Date Mon Aug 17 11:30:00 PDT 2009> 

If you have a Java object with your own Java serialization method, you can simply use it and not write your own code for this.

+9
source share

All Articles