 (defmethod encode :clojure [session message options]
   "Stringify a clojure data structure"
   (set-encoding
-   (.createTextMessage session (with-out-str
-                                 (binding [*print-dup* true]
-                                   (pr message))))
+   (.createTextMessage session (core/encode message))
    :clojure))
 
 (defmethod encode :json [session message options]
   "Stringify a json data structure"
   (set-encoding
-   (.createTextMessage session (json/json-str message))
+   (.createTextMessage session (core/encode message :json))
    :json))
 
 (defmethod encode :text [session message options]


(defmethod decode :default [message]
  (throw (RuntimeException. (str "Received unknown message encoding: " (get-encoding message)))))


;; Fallback to Java serialization for undefined print-dup methods

(defn serialize [object]
  (with-open [baos (java.io.ByteArrayOutputStream.)
              oos (java.io.ObjectOutputStream. baos)]
    (.writeObject oos object)
    (.toString baos "ISO-8859-1")))

(defn deserialize [s]
  (with-open [bais (java.io.ByteArrayInputStream. (.getBytes s "ISO-8859-1"))
              ois (java.io.ObjectInputStream. bais)]
    (.readObject ois)))

(defmethod print-dup :default [o w]
  (.println System/out (str "WARN: using Java serialization for " (.getName (class o))))
  (.write w "#=(immutant.messaging.codecs/deserialize ")
  (print-dup (serialize o) w)
  (.write w ")"))


(defmethod decode :clojure [message]
  "Turn a string into a clojure data structure"
  (core/decode (.getText message)))

(defmethod decode :json [message]
  "Turn a string into a json data structure"
  (core/decode (.getText message) :json))

(defmethod decode :text [message]
  "Treats the message payload as a raw string. No decoding is done."
