clojure

Drew Verlee 2025-08-13T17:19:55.170449Z

i have an api thats handing back fields that start with @, which can't be converted to :@ using clj-https :as :json output coercian, now, cheshire says it can do a custom coerce (e.g "@id" -> "id"), which is maybe what i want, but i can't seem to find good examples of it.

rolt 2025-08-14T08:47:18.350989Z

there's an example for using a custom key-fn in clj-http: https://github.com/dakrone/clj-http/blob/master/examples/body_coercion.clj

rolt 2025-08-14T08:49:14.706879Z

or decoding the body yourself something like that with chesire: (json/parse-string "{\"@id\": 1}" (fn [s] (if (= (first s) \@) (subs s 1) s)))

🙏 1
p-himik 2025-08-13T17:24:23.631319Z

That might be another thing worthy of an FAQ entry. :) Just don't convert random strings to keywords. It's both simple and easy. :strs exists as a replacement for :keys. (get m "a") is not much worse than (:a m) (and (m "a") also works if m is guaranteed to be not nil). If you don't want to proliferate strings, hide them behind functions, which is probably a good practice for keywords just as well, so you don't end up having (get-in m [:a :b :c]) in 15 different places.

➕ 1
Drew Verlee 2025-08-13T17:32:37.070259Z

Thanks I guess i don't understand what you mean by random, the json has string fields whose values i want, and keywords make that easier then strings. I'm not currently using whatever the json author thought "@" meant and it's conflict with the clojure runtime seems like an incidental translation issue of the :as :json functionality attached to clj-http. That being said, i probably will give up on this for the moment as i don't see an easy way, and to your point, working with strings isn't that much harder.

p-himik 2025-08-13T17:38:10.478229Z

Given that it's @, :strs won't help much unfortunately so it was my mistake in mentioning it. But you still have get and calling maps as functions. You still can write a tiny let-like macro similar to (str-let [{:keys [a b c]} m] ...) that expands into (let [a (get m "@a"), b (get m "@b"), c (get m "@c")] ...). Yet another approach would be to create something similar to https://github.com/mfikes/cljs-bean but for maps with strings, with custom converters. Actually sounds like a potentially neat and useful project.

👀 1
Drew Verlee 2025-08-13T17:39:40.764799Z

yeah, i can get by with get-in lol.

Drew Verlee 2025-08-13T17:40:23.038479Z

it's just annoying after you drill down 3 branches in a json object to suddenly hit this and have to redo all the code.

p-himik 2025-08-13T17:43:25.080749Z

Another reason to keep all the get-ins in a single location. :) Behind some interface that you don't have to change even if the underlying data becomes different or starts requiring a different approach for some reason. But yeah, that's a separate topic.

👍 1