liberator

rads 2016-09-11T19:41:14.000003Z

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

rads 2016-09-11T19:42:44.000004Z

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}))

ordnungswidrig 2016-09-11T20:08:33.000006Z

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