Skip to content

Instantly share code, notes, and snippets.

@dakrone
Last active August 5, 2019 10:55
Show Gist options
  • Save dakrone/5577106 to your computer and use it in GitHub Desktop.
Save dakrone/5577106 to your computer and use it in GitHub Desktop.
(ns cheshire.experimental
(:require [cheshire.core :refer :all]
[clojure.java.io :refer :all])
(:import (java.io ByteArrayInputStream FilterInputStream
SequenceInputStream)))
(defn escaping-input-stream
[is]
(let [new-is (proxy [FilterInputStream] [is]
(read
([]
(println :read)
(.read is))
([b]
(println :readd)
(.read is b))
([buff off len]
(println :readdd (type buff) :len len :off off)
(let [newb (byte-array (* 2 len))
res (.read is newb off len)
offset (atom 0)]
(println :res res)
(when-not (= -1 res)
(loop [b newb]
(let [bit (first (take 1 b))
c (char bit)]
(prn :c c)
(case c
\\ (do
(println :escape! :slash)
(aset buff @offset (byte \\))
(aset buff (inc @offset) (byte \\))
(swap! offset inc)
(swap! offset inc))
\" (do
(println :escape! :quote)
(aset buff @offset (byte \\))
(aset buff (inc @offset) (byte \"))
(swap! offset inc)
(swap! offset inc))
(do
(aset buff @offset (byte c))
(swap! offset inc)))
(if (zero? bit)
(println :bit-zero :dropping-out)
(recur (drop 1 b)))))
(println :final @offset :res res))
(println :final2 @offset :res res)
(if (= -1 res)
-1
(dec @offset)))))
(close []
(println :closing)
(proxy-super close)))]
new-is))
(defn doit []
(let [i2 (escaping-input-stream (input-stream (file "/tmp/foo")))
json (str "{\"body\":\"" (slurp i2) "\"}")
_ (prn :json-raw json)
_ (println :json json)
thing (decode json true)
_ (prn :thing-raw thing)
_ (println :thing thing)]))
(defn encode-large-field-in-map [obj field filename]
(let [otherstr (encode (dissoc obj field))
truncstr (str (subs otherstr 0 (dec (count otherstr))))
stream (escaping-input-stream (input-stream (file filename)))
pre-stream (ByteArrayInputStream.
(.getBytes (str truncstr ",\"" (name field) "\":\"")))
post-stream (ByteArrayInputStream. (.getBytes "\"}"))]
(SequenceInputStream. (SequenceInputStream. pre-stream stream)
post-stream)))
;; es.streams> (slurp (encode-large-field-in-map {:id "10" :count 5 :thing ["1 2 3"] :body "..."} :body "/tmp/foo"))
;; :readdd [B :len 8192 :off 0
;; :res 74
;; :c \h
;; :c \i
;; :c \,
;; :c \space
;; :c \t
;; :c \h
;; :c \i
;; :c \s
;; :c \space
;; :c \i
;; :c \s
;; :c \space
;; :c \s
;; :c \o
;; :c \m
;; :c \e
;; :c \space
;; :c \s
;; :c \t
;; :c \u
;; :c \f
;; :c \f
;; :c \.
;; :c \space
;; :c \tab
;; :c \I
;; :c \t
;; :c \space
;; :c \h
;; :c \a
;; :c \s
;; :c \space
;; :c \a
;; :c \space
;; :c \"
;; :escape! :quote
;; :c \w
;; :c \o
;; :c \r
;; :c \d
;; :c \"
;; :escape! :quote
;; :c \space
;; :c \i
;; :c \n
;; :c \space
;; :c \i
;; :c \t
;; :c \.
;; :c \space
;; :c \a
;; :c \n
;; :c \d
;; :c \space
;; :c \a
;; :c \space
;; :c \\
;; :escape! :slash
;; :c \.
;; :c \space
;; :c \b
;; :c \u
;; :c \t
;; :c \space
;; :c \t
;; :c \h
;; :c \a
;; :c \t
;; :c \'
;; :c \s
;; :c \space
;; :c \c
;; :c \o
;; :c \o
;; :c \l
;; :c \.
;; :c \newline
;; :c \^@
;; :bit-zero :dropping-out
;; :final 78 :res 74
;; :final2 78 :res 74
;; :readdd [B :len 8192 :off 0
;; :res -1
;; :final2 0 :res -1
;; :closing
;; "{\"count\":5,\"thing\":[\"1 2 3\"],\"id\":\"10\",\"body\":\"hi, this is some stuff. \tIt has a \\\"word\\\" in it. and a \\\\. but that's cool.\n\"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment