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

:key-fn for encoding keys with babashka.json/write-str #10

Open
rads opened this issue Nov 8, 2024 · 8 comments
Open

:key-fn for encoding keys with babashka.json/write-str #10

rads opened this issue Nov 8, 2024 · 8 comments

Comments

@rads
Copy link
Contributor

rads commented Nov 8, 2024

Would it make sense to add :key-fn for babashka.json/write-str for encoding keys since this is already supported for decoding keys with read-str?

@rads
Copy link
Contributor Author

rads commented Nov 8, 2024

Right now I'm using babashka.json with an HTTP API that uses snake_case.

This works:

(json/read-str s {:key-fn inflections/hyphenate})

But not this:

(json/write-str x {:key-fn inflections/underscore})

@borkdude
Copy link
Contributor

borkdude commented Nov 8, 2024

I guess so, if the underlying libraries support it too?

@rads
Copy link
Contributor Author

rads commented Nov 8, 2024

I can check if any of the libraries don't support this and get back to you.

@rads
Copy link
Contributor Author

rads commented Nov 8, 2024

@borkdude: I wrote a test to show how this is done for each lib (see README to run it yourself):

https://github.com/rads/json-key-fns/blob/main/src/rads/json_key_fns.clj

The only one that doesn't support it easily is charred. Overriding the behavior requires a custom :obj-fn and the default-obj-fn is private so I had to copy-paste the implementation and modify it slightly. There are a handful of options to move forward here:

  1. Do nothing and close this issue
  2. Use the custom obj-fn with the copy-pasted code
  3. Throw an exception when using :key-fn with json/write-str when using charred
  4. Coerce the keys in babashka.json before calling charred.api/write-json-str
  5. Talk to @cnuernber to see if there is a simpler way or a possible improvement we can make to charred to make this easier

If we want to move forward with this, I can follow up with PRs and reach out to @cnuernber if needed. If it's not worth the effort, I'll go ahead and close this issue.

@rads
Copy link
Contributor Author

rads commented Nov 8, 2024

Added another option to the list above to throw an exception if using :key-fn with charred.

@rads
Copy link
Contributor Author

rads commented Nov 8, 2024

And one more option for coercing the keys before calling charred. 😄

@borkdude
Copy link
Contributor

borkdude commented Nov 8, 2024 via email

@rads
Copy link
Contributor Author

rads commented Nov 8, 2024

Sounds good, I'll get back to you when I have some more info.

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

2 participants