Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacervello committed Aug 11, 2017
1 parent 513e437 commit 1cd2760
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/cheshire/generate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
(clojure.lang IPersistentCollection Keyword Ratio Symbol)))

;; date format rebound for custom encoding
(def ^{:dynamic true :private true} *date-format* "yyyy-MM-dd'T'HH:mm:ssZ")
(def ^{:dynamic true :private true} *date-format*)

(defmacro tag
([obj]
Expand Down
48 changes: 18 additions & 30 deletions src/cheshire/parse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,9 @@
:dynamic true}
*use-bigdecimals?* false)

(def ^{:dynamic true} *valid-json-only* false)

(defmacro ^:private tag
([obj]
`(vary-meta ~obj assoc :tag `JsonParser)))

(defmacro valid [parsed]
`(if *valid-json-only*
(let [valid-json?# (try (nil? (.nextToken ~'jp))
(catch Exception _# false))]
(if-not valid-json?#
(throw (Exception. "Invalid json"))
~parsed))
~parsed))
`(vary-meta ~obj assoc :tag `JsonParser)))

(definline parse-object [^JsonParser jp key-fn bd? array-coerce-fn]
(let [jp (tag jp)]
Expand Down Expand Up @@ -87,24 +76,23 @@
(str "Cannot parse " (pr-str (.getCurrentToken jp)))))))

(defn parse-strict [^JsonParser jp key-fn eof array-coerce-fn]
(let [key-fn (or (if (identical? key-fn true) keyword key-fn) identity)
_ (.nextToken jp)
parsed (condp identical? (.getCurrentToken jp)
nil
eof
(parse* jp key-fn *use-bigdecimals?* array-coerce-fn))]
(valid parsed)))
(let [key-fn (or (if (identical? key-fn true) keyword key-fn) identity)]
(.nextToken jp)
(condp identical? (.getCurrentToken jp)
nil
eof
(parse* jp key-fn *use-bigdecimals?* array-coerce-fn))))

(defn parse [^JsonParser jp key-fn eof array-coerce-fn]
(let [key-fn (or (if (and (instance? Boolean key-fn) key-fn) keyword key-fn) identity)
_ (.nextToken jp)
parsed (condp identical? (.getCurrentToken jp)
nil
eof
(let [key-fn (or (if (and (instance? Boolean key-fn) key-fn) keyword key-fn) identity)]
(.nextToken jp)
(condp identical? (.getCurrentToken jp)
nil
eof

JsonToken/START_ARRAY
(do
(.nextToken jp)
(lazily-parse-array jp key-fn *use-bigdecimals?* array-coerce-fn))

JsonToken/START_ARRAY
(do
(.nextToken jp)
(lazily-parse-array jp key-fn *use-bigdecimals?* array-coerce-fn))
(parse* jp key-fn *use-bigdecimals?* array-coerce-fn))]
(valid parsed)))
(parse* jp key-fn *use-bigdecimals?* array-coerce-fn))))

0 comments on commit 1cd2760

Please sign in to comment.