Skip to content

Commit

Permalink
Revert "Converting byte array to string before json deserialization"
Browse files Browse the repository at this point in the history
This reverts commit 8c0f868.
  • Loading branch information
mjayprateek committed Oct 14, 2019
1 parent 86c8da1 commit 64e6d33
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
10 changes: 4 additions & 6 deletions src/ziggurat/middleware/json.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
[ziggurat.metrics :as metrics]))

(defn- deserialize-json
[message topic-entity-name key-fn encoding]
[message topic-entity-name key-fn]
(try
(parse-string (String. message encoding) key-fn)
(parse-string message key-fn)
(catch Exception e
(let [additional-tags {:topic_name topic-entity-name}
default-namespace "json-message-parsing"]
Expand Down Expand Up @@ -39,9 +39,7 @@
"
([handler-fn topic-entity-name]
(parse-json handler-fn topic-entity-name true "UTF-8"))
(parse-json handler-fn topic-entity-name true))
([handler-fn topic-entity-name key-fn]
(parse-json handler-fn topic-entity-name key-fn "UTF-8"))
([handler-fn topic-entity-name key-fn encoding]
(fn [message]
(handler-fn (deserialize-json message topic-entity-name key-fn encoding)))))
(handler-fn (deserialize-json message topic-entity-name key-fn)))))
20 changes: 4 additions & 16 deletions test/ziggurat/middleware/json_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
handler-fn (fn [msg]
(if (= msg message)
(reset! handler-fn-called? true)))]
((parse-json handler-fn topic-entity-name) (.getBytes (generate-string message)))
((parse-json handler-fn topic-entity-name) (generate-string message))
(is (true? @handler-fn-called?))))
(testing "Given a handler function and key-fn as false, parse-json should call that function on after
deserializing the string without coercing the keys to keywords."
Expand All @@ -29,19 +29,7 @@
handler-fn (fn [msg]
(if (= msg expected-output)
(reset! handler-fn-called? true)))]
((parse-json handler-fn topic-entity-name false) (.getBytes (generate-string message)))
(is (true? @handler-fn-called?))))
(testing "Given a handler function, key-fn as false and an encoding, parse-json should call that function on after
deserializing the string (with the given encoding) without coercing the keys to keywords."
(let [handler-fn-called? (atom false)
message {:a "A"
:b "B"}
expected-output {"a" "A" "b" "B"}
topic-entity-name "test"
handler-fn (fn [msg]
(if (= msg expected-output)
(reset! handler-fn-called? true)))]
((parse-json handler-fn topic-entity-name false "UTF-8") (.getBytes (generate-string message)))
((parse-json handler-fn topic-entity-name false) (generate-string message))
(is (true? @handler-fn-called?))))
(testing "Given a handler function and a key-fn, parse-json should call that function after
deserializing the string by applying key-fn to keys."
Expand All @@ -54,7 +42,7 @@
handler-fn (fn [msg]
(if (= msg expected-output)
(reset! handler-fn-called? true)))]
((parse-json handler-fn topic-entity-name key-fn) (.getBytes (generate-string message)))
((parse-json handler-fn topic-entity-name key-fn) (generate-string message))
(is (true? @handler-fn-called?))))
(testing "Should report metrics when JSON deserialization fails"
(let [handler-fn-called? (atom false)
Expand All @@ -66,6 +54,6 @@
(reset! handler-fn-called? true)))]
(with-redefs [metrics/increment-count (fn [_ _ _]
(reset! metric-reporter-called? true))]
((parse-json handler-fn topic-entity-name true) (.getBytes message)))
((parse-json handler-fn topic-entity-name true) message))
(is (true? @handler-fn-called?))
(is (true? @metric-reporter-called?)))))

0 comments on commit 64e6d33

Please sign in to comment.