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

Support some kind of fallback encoder #161

Open
rafaeldff opened this issue Jan 13, 2020 · 3 comments
Open

Support some kind of fallback encoder #161

rafaeldff opened this issue Jan 13, 2020 · 3 comments

Comments

@rafaeldff
Copy link

My use case is to serialize arbitrary clojure data for logging purposes in a library. I cannot guarantee that data will be acceptable by Cheshire (it can include atoms, functions or other objects). On the other hand, it's fine to just include the .toString value as a json string.

I could build on the support for custom encoders, and define an encoder for java.lang.object such as:

(extend-protocol cheshire.generate/JSONable
    Object
    (to-json [this jsonGenerator]
      (.writeString jsonGenerator (str this))))

The issue with this approach is that it changes cheshire's behavior for the entire program, which is not acceptable in a library (no one expects their logging library to change how it serializes http responses, for instance).

If I could pass a fallback encoder function as an option to generate-string, the scope of the extension would be local to the library.

As another alternative, I could build upon #118 and recurse through the data (maybe with postwalk) before sending it out to cheshire, but I worry about the performance impact.

@camdez
Copy link

camdez commented Feb 22, 2020

I have precisely the same use case / issue.

@stathissideris
Copy link

Same for me! cheshire.generate/generate could support a fallback option!

@viebel
Copy link

viebel commented Nov 22, 2020

Same for me!
Has anyone found a descent workaround?

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

No branches or pull requests

4 participants