Created
August 18, 2017 05:29
-
-
Save mattly/217eb6f26cb5d728a6cc88b4d6b926bb to your computer and use it in GitHub Desktop.
Quick util for reading/writing transit json in clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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