Hello noahr,

I have created a library for myself to do persistence in Clojure to
Berkeley DB. I generally leverage Clojure's reader support for
(de)serialization.

However, in one instance I had to implement a particular interface and
pass that class as parameter to the Berkeley API.

At first I, like you, used the proxy form, and, even though I
indicated implementation of the required interface, I also got the
error message that PersistentMap is not serializable.

After some doodling I found a solution that obviates the need to write
Java code.

Simply use the "get-xxx-class-xxx" family of forms.

For example, in my case I needed to supply a class that implements
java.util.Comparator (with concomitant serializable requirements on
the Berkeley API side). I did it roughly like so:

(in-ns 'myLib.CalendarComparator)
(clojure/refer 'clojure)

(defn- deserialize [bytes]
  (let [stream (ObjectInputStream. (ByteArrayInputStream. bytes))]
    (let [result (.readObject stream)]
      (.close stream)
      result)))

(defn compare [time1bytes time2bytes]
  (let [t1 (deserialize time1bytes)
        t2 (deserialize time2bytes)]
    (if (.before t1 t2)
      -1
      (if (.after t1 t2)
        1
        0))))


;;; Declare class and make available as myLib.CalendarComparator

(try (clojure/gen-and-load-class 'mylib.CalendarComparator
                                 :implements [java.util.Comparator])
     (catch java.lang.LinkageError e))


=====

When loading the above code, I can now pass myLib.CalendarComparator
as class to the Berkeley API and no longer get the error that
PersistentMap is not serializable.

Joubert

On Sep 12, 12:59 pm, noahr <[EMAIL PROTECTED]> wrote:
> Well digging into it more, I've since realized the whole approach I
> was taking is misguided anyway, as serialization appears to be for
> marshaling the data of already defined classes.  What I was really
> trying to do was create new classes (java or clojure), and stream them
> across the network, and have them executed on other side by a system
> that only knew the 'parent' class or interface. But I believe this
> will require custom class loading from bytes on the other side.
>
> I still suspect though there will be issues when trying to mix dynamic
> clojure-created classes with serialization, due to its data types not
> being serializable. Some of the customizations for java serialization
> might offer a way out, but uncertain..
>
> Also, I don't have clojure on both sides; unfamiliar with thrift/
> protocol buffers, will look them up.
>
> Thx --n
>
> > If you've got Clojure available on both ends, you can serialize with
> > (pr-str...) and deserialize with (read...).  Only works with pure-
> > Clojure data structures, tho.
>
> > Maybe you could use Clojure to generate a native Java collection like
> > ArrayList and serialize that.
>
> > Another option is something like Thrift or Protocol Buffers.
--~--~---------~--~----~------------~-------~--~----~
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