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

Conversation

bgrabow
Copy link

@bgrabow bgrabow commented Oct 25, 2019

(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"})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants