Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind dynamic date-format var for custom encoders #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cheshire/generate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@
(defn generate [^JsonGenerator jg obj ^String date-format ^Exception ex key-fn]
(cond
(nil? obj) (.writeNull ^JsonGenerator jg)
(get (:impls JSONable) (class obj)) (#'to-json obj jg)

(get (:impls JSONable) (class obj))
(binding [*date-format* date-format]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the binding should be done at a higher level, in core.clj rather than here, since that is where it's passed in. What do you think?

Copy link
Author

@bgrabow bgrabow Nov 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I see a reason to expose the details of the *date-format* var to namespaces outside of cheshire.generate. I chose to set the binding where I did so that the scope of the binding is as narrow as possible. Right now the only contexts I see that have reason to consume the value of *date-format* are the client-defined JSONable encoding functions, so the location the binding is set in my suggested version limits the binding to only that context. Keeping the scope of the binding narrow minimizes unnecessary coupling to other namespaces and functions and will keep the design more modular.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dakrone Any further thoughts on this PR? It may not matter much how this is implemented as long as the caller can do something like this and it works:

;; Somewhere in configuration code
(gen/add-encoder DateTime 
  (fn [^DateTime dt ^JsonGenerator jg]
    (gen/encode-date (.toDate dt) jg))

;; Somewhere in application event handler
(json/encode {:date (org.joda.time.DateTime/now)}
             {:date-format "yyyy-MM-dd"})

(#'to-json obj jg))

(i? clojure.lang.IPersistentMap obj)
(generate-map jg obj date-format ex key-fn)
Expand Down
3 changes: 3 additions & 0 deletions test/cheshire/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@
(binding [gen/*date-format* "yyyy-MM-dd'T'HH:mm:ss'Z'"]
(is (= "1970-01-01T00:00:00Z" (json/decode (json/encode thing) true))))
(remove)
(gen/add-encoder CTestR (fn [obj jg] (gen/encode-date (Date. (long 0)) jg)))
(is (= "1970-01-01" (json/decode (json/encode thing {:date-format "yyyy-MM-dd"}))))
(remove)
(gen/add-encoder CTestR (fn [obj jg] (gen/encode-bool true jg)))
(is (= true (json/decode (json/encode thing) true)))
(remove)
Expand Down