Skip to content

Instantly share code, notes, and snippets.

@mattly
Created August 18, 2017 05:29
Show Gist options
  • Save mattly/217eb6f26cb5d728a6cc88b4d6b926bb to your computer and use it in GitHub Desktop.
Save mattly/217eb6f26cb5d728a6cc88b4d6b926bb to your computer and use it in GitHub Desktop.
Quick util for reading/writing transit json in clojure
(ns myapp.util.transit
(:require
[clojure.java.io :as io]
[cognitect.transit :as transit])
(:import
[java.io ByteArrayOutputStream]))
(defn- ->stream [input]
(cond (string? input) (io/input-stream (.getBytes input))
:default input))
(defn read-json [input]
(with-open [ins (->stream input)]
(-> ins
(transit/reader :json)
transit/read)))
(defn write-json [output]
(let [out (ByteArrayOutputStream. 4096)
r (transit/writer out :json)
_ (transit/write r output)
ret (.toString out)]
(.reset out)
ret))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment