Skip to content

Commit

Permalink
Merge pull request #174 from nilern/reduce
Browse files Browse the repository at this point in the history
Replace `doseq` with `reduce`
  • Loading branch information
dakrone authored Mar 17, 2021
2 parents 44f3785 + 7040a4a commit d2153a6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 61 deletions.
21 changes: 12 additions & 9 deletions src/cheshire/custom.clj
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@
"Encode a seq to the json generator."
[s ^JsonGenerator jg]
(.writeStartArray jg)
(doseq [i s]
(to-json i jg))
(reduce (fn [jg i] (to-json i jg) jg) jg s)
(.writeEndArray jg))

(defn encode-date
Expand All @@ -182,13 +181,17 @@
"Encode a clojure map to the json generator."
[^clojure.lang.IPersistentMap m ^JsonGenerator jg]
(.writeStartObject jg)
(doseq [[k v] m]
(.writeFieldName jg (if (instance? clojure.lang.Keyword k)
(if-let [ns (namespace k)]
(str ns "/" (name k))
(name k))
(str k)))
(to-json v jg))
(reduce (fn [^JsonGenerator jg kv]
(let [k (key kv)
v (val kv)]
(.writeFieldName jg (if (instance? clojure.lang.Keyword k)
(if-let [ns (namespace k)]
(str ns "/" (name k))
(name k))
(str k)))
(to-json v jg)
jg))
jg m)
(.writeEndObject jg))

(defn encode-symbol
Expand Down
58 changes: 32 additions & 26 deletions src/cheshire/generate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@
(let [jg (tag jg)]
`(do
(.writeStartObject ~jg)
(doseq [m# ~obj]
(let [k# (key m#)
v# (val m#)]
(.writeFieldName ~jg (if (keyword? k#)
(.substring (str k#) 1)
(str k#)))
(generate ~jg v# ~date-format ~e nil)))
(reduce (fn [^JsonGenerator jg# kv#]
(let [k# (key kv#)
v# (val kv#)]
(.writeFieldName jg# (if (keyword? k#)
(.substring (str k#) 1)
(str k#)))
(generate jg# v# ~date-format ~e nil)
jg#))
~jg ~obj)
(.writeEndObject ~jg))))

(definline generate-key-fn-map
Expand All @@ -77,14 +79,16 @@
jg (tag jg)]
`(do
(.writeStartObject ~jg)
(doseq [m# ~obj]
(let [~k (key m#)
v# (val m#)
^String name# (if (keyword? ~k)
(~key-fn ~k)
(str ~k))]
(.writeFieldName ~jg name#)
(generate ~jg v# ~date-format ~e ~key-fn)))
(reduce (fn [^JsonGenerator jg# kv#]
(let [~k (key kv#)
v# (val kv#)
^String name# (if (keyword? ~k)
(~key-fn ~k)
(str ~k))]
(.writeFieldName jg# name#)
(generate jg# v# ~date-format ~e ~key-fn)
jg#))
~jg ~obj)
(.writeEndObject ~jg))))

(definline generate-map
Expand All @@ -98,8 +102,7 @@
(let [jg (tag jg)]
`(do
(.writeStartArray ~jg)
(doseq [item# ~obj]
(generate ~jg item# ~date-format ~e ~key-fn))
(reduce (fn [jg# item#] (generate jg# item# ~date-format ~e ~key-fn) jg#) ~jg ~obj)
(.writeEndArray ~jg))))

(defmacro i?
Expand Down Expand Up @@ -188,8 +191,7 @@
"Encode a seq to the json generator."
[s ^JsonGenerator jg]
(.writeStartArray jg)
(doseq [i s]
(generate jg i *date-format* nil nil))
(reduce (fn [jg i] (generate jg i *date-format* nil nil) jg) jg s)
(.writeEndArray jg))

(defn encode-date
Expand All @@ -215,13 +217,17 @@
"Encode a clojure map to the json generator."
[^clojure.lang.IPersistentMap m ^JsonGenerator jg]
(.writeStartObject jg)
(doseq [[k v] m]
(.writeFieldName jg (if (instance? clojure.lang.Keyword k)
(if-let [ns (namespace k)]
(str ns "/" (name k))
(name k))
(str k)))
(generate jg v *date-format* nil nil))
(reduce (fn [^JsonGenerator jg kv]
(let [k (key kv)
v (val kv)]
(.writeFieldName jg (if (instance? clojure.lang.Keyword k)
(if-let [ns (namespace k)]
(str ns "/" (name k))
(name k))
(str k)))
(generate jg v *date-format* nil nil)
jg))
jg m)
(.writeEndObject jg))

(defn encode-symbol
Expand Down
58 changes: 32 additions & 26 deletions src/cheshire/generate_seq.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@
(let [jg (tag jg)]
`(do
(write-start-object ~jg ~wholeness)
(doseq [m# ~obj]
(let [k# (key m#)
v# (val m#)]
(.writeFieldName ~jg (if (keyword? k#)
(.substring (str k#) 1)
(str k#)))
(generate ~jg v# ~date-format ~e nil
:wholeness (if (= ~wholeness :start-inner)
:start
:all))))
(reduce (fn [^JsonGenerator jg# kv#]
(let [k# (key kv#)
v# (val kv#)]
(.writeFieldName jg# (if (keyword? k#)
(.substring (str k#) 1)
(str k#)))
(generate jg# v# ~date-format ~e nil
:wholeness (if (= ~wholeness :start-inner)
:start
:all))
jg#))
~jg ~obj)
(write-end-object ~jg ~wholeness))))

(definline generate-key-fn-map
Expand All @@ -55,17 +57,19 @@
jg (tag jg)]
`(do
(write-start-object ~jg ~wholeness)
(doseq [m# ~obj]
(let [~k (key m#)
v# (val m#)
^String name# (if (keyword? ~k)
(~key-fn ~k)
(str ~k))]
(.writeFieldName ~jg name#)
(generate ~jg v# ~date-format ~e ~key-fn
:wholeness (if (= ~wholeness :start-inner)
:start
:all))))
(reduce (fn [^JsonGenerator jg# kv#]
(let [~k (key kv#)
v# (val kv#)
^String name# (if (keyword? ~k)
(~key-fn ~k)
(str ~k))]
(.writeFieldName jg# name#)
(generate jg# v# ~date-format ~e ~key-fn
:wholeness (if (= ~wholeness :start-inner)
:start
:all))
jg#))
~jg ~obj)
(write-end-object ~jg ~wholeness))))

(definline generate-map
Expand All @@ -80,11 +84,13 @@
(let [jg (tag jg)]
`(do
(write-start-array ~jg ~wholeness)
(doseq [item# ~obj]
(generate ~jg item# ~date-format ~e ~key-fn
:wholeness (if (= ~wholeness :start-inner)
:start
:all)))
(reduce (fn [jg# item#]
(generate jg# item# ~date-format ~e ~key-fn
:wholeness (if (= ~wholeness :start-inner)
:start
:all))
jg#)
~jg ~obj)
(write-end-array ~jg ~wholeness))))

(defn generate [^JsonGenerator jg obj ^String date-format
Expand Down

0 comments on commit d2153a6

Please sign in to comment.