Fork me on GitHub
#liberator
<
2016-09-11
>
rads19:09:14

I'm trying to get Liberator to send UUIDs as strings when JSON is the content type. right now I'm getting this error:

Don't know how to write JSON of class java.util.UUID

rads19:09:44

I tried to solve the problem with this code, but it doesn't seem to have any effect. am I missing something here?

(ns server
  (:require [liberator.representation :as lib-rep]
            [clojure.data.json :as json]))

(defn uuid->str [key value]
  (if (instance? java.util.UUID value)
    (str value)
    value))

(remove-method lib-rep/render-map-generic "application/json")

(defmethod lib-rep/render-map-generic "application/json" [data context]
  (json/write-str data {:value-fn uuid->str}))

ordnungswidrig20:09:33

I would use a custom function for :as-response that delegates to the default as-response for non-JSON types.