Skip to content
Snippets Groups Projects
Commit d66f9eae authored by Cam Saul's avatar Cam Saul
Browse files

Handle PGobjects when serializing JSON (e.g. raw data that contains

PostGIS geometries)
parent 610a9dde
Branches
Tags
No related merge requests found
(ns metabase.middleware.format
(:require [clojure.core.match :refer [match]]
cheshire.factory
(cheshire factory
[generate :refer [add-encoder encode-str]])
[medley.core :refer [filter-vals map-vals]]
[metabase.util :as util]))
(declare -format-response)
;; # SHADY HACK
;; ## SHADY HACK
;; Tell the JSON middleware to use a date format that includes milliseconds
(intern 'cheshire.factory 'default-date-format "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
;; # FORMAT RESPONSE MIDDLEWARE
;; ## Custom JSON encoders
(add-encoder org.h2.jdbc.JdbcClob (fn [clob ^com.fasterxml.jackson.core.JsonGenerator json-generator] ; stringify JDBC Clobs
(.writeString json-generator (util/jdbc-clob->str clob))))
(add-encoder org.postgresql.util.PGobject encode-str) ; stringify Postgres binary objects (e.g. PostGIS geometries)
;; ## FORMAT RESPONSE MIDDLEWARE
(defn format-response
"Middleware that recurses over Clojure object before it gets converted to JSON and makes adjustments neccessary so the formatter doesn't barf.
e.g. functions and delays are stripped and H2 Clobs are converted to strings."
......@@ -21,8 +30,8 @@
(defn- remove-fns-and-delays
"Remove values that are fns or delays from map M."
[m]
(filter-vals #(not (or (delay? %)
(fn? %)))
(filter-vals #(not (or (fn? %)
(delay? %)))
m))
(defn- clob? [obj]
......@@ -33,5 +42,4 @@
(map? obj) (->> (remove-fns-and-delays obj) ; recurse over all vals in the map
(map-vals -format-response))
(coll? obj) (map -format-response obj) ; recurse over all items in the collection
(clob? obj) (util/jdbc-clob->str obj)
:else obj))
......@@ -107,11 +107,11 @@
(defn jdbc-clob->str
"Convert a `JdbcClob` to a `String`."
([clob]
(^String
[clob]
(when clob
(if (string? clob) clob
(->> (-> (.getCharacterStream ^org.h2.jdbc.JdbcClob clob)
(jdbc-clob->str []))
(->> (jdbc-clob->str (.getCharacterStream ^org.h2.jdbc.JdbcClob clob) [])
(interpose "\n")
(apply str)))))
([^java.io.BufferedReader reader acc]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment