(ns pocketbook.transit "Transit encoding/decoding helpers for the HTTP wire format." (:require [cognitect.transit :as t]) (:import [java.io ByteArrayInputStream ByteArrayOutputStream])) (defn encode "Encode a Clojure value to a Transit+JSON byte array." [v] (let [out (ByteArrayOutputStream. 4096) w (t/writer out :json)] (t/write w v) (.toByteArray out))) (defn encode-str "Encode a Clojure value to a Transit+JSON string." [v] (let [out (ByteArrayOutputStream. 4096) w (t/writer out :json)] (t/write w v) (.toString out "UTF-8"))) (defn decode "Decode a Transit+JSON byte array or input stream to a Clojure value." [input] (let [in (cond (instance? ByteArrayInputStream input) input (instance? java.io.InputStream input) input (bytes? input) (ByteArrayInputStream. input) (string? input) (ByteArrayInputStream. (.getBytes ^String input "UTF-8")) :else (throw (ex-info "Cannot decode, unsupported input type" {:type (type input)})))] (t/read (t/reader in :json))))