ring

Jacob Rosenzweig 2021-07-27T01:32:48.033500Z

Can anyone tell me why this decoder function works but not the encoder function?

(def instance
  (m/create
    (-> m/default-options
        (update-in
          [:formats "application/transit+json" :decoder-opts]
          (partial merge time/time-deserialization-handlers))
        (update-in
          [:formats "application/transit+json" :encoder-opts]
          (partial merge time/time-serialization-handlers))
        (update-in
          [:formats "application/json" :encoder-opts]
          (partial merge {:encode-key-fn #(name (csk/->camelCase %))}))
        (update-in
          [:formats "application/json" :decoder-opts]
          (partial merge {:decode-key-fn #(keyword (csk/->kebab-case %))})))))

Jacob Rosenzweig 2021-07-27T01:33:04.033900Z

I still get camel case in my web responses.

seancorfield 2021-07-27T01:55:31.034600Z

@rosenjcb It would help if you explained what m and time were and how this instance is used.

Jacob Rosenzweig 2021-07-27T01:58:52.037500Z

So for context, this is what I get out of the luminus template. I honestly am not sure where this belongs, so I put it in the #ring channel as a muuntaja channel doesn't exist. As far as I can tell, it's used by the Muuntaja JSON middleware. One suggestion someone had was this: https://github.com/duct-framework/duct/issues/104 Which basically boils down to adding custom decoder and encoder functions to the format config. My encoder works, it's able to call the (csk/->kebab-case) function and convert any camelCase payloads into kebab-case maps. But for whatever reason, it doesn't want to work the other way around.

Jacob Rosenzweig 2021-07-27T01:59:01.037900Z

Maybe my function is wrong, maybe my approach is wrong. Not really sure.

seancorfield 2021-07-27T02:04:42.038800Z

This is why I don't recommend Luminus for beginners -- so many moving parts and so many libraries to understand... and so many opportunities for things to go wrong 😐

seancorfield 2021-07-27T02:07:20.040900Z

Auto-converting JSON to keywords is generally considered a bad idea (although it is less dangerous than it used to be: since keywords were interned "forever" you could DDoS Clojure servers by just sending them lots of random JSON and filling up their keyword storage -- but now I believe it uses weak references so they can be GC'd?). Anyways, just something to be aware of.

👀 1
seancorfield 2021-07-27T02:09:31.043100Z

I've never used Muuntaja. That's a Metosin library? Are you also using Reitit? There's a #reitit channel so maybe you could ask in there. I thought there was a #metosin channel but apparently not. There is a #duct channel which might be worth a try (I've never used Duct either).

seancorfield 2021-07-27T02:10:16.044Z

Part of the "problem" with the Luminus template is that there are so many layers and you kind of have to understand them all in order to start customizing stuff...

seancorfield 2021-07-27T02:10:33.044500Z

There is a #luminus channel too...

Jacob Rosenzweig 2021-07-27T02:12:47.045Z

Yeah let me try reposting in #reitit. Seems like there's some activity there.

Jacob Rosenzweig 2021-07-27T02:13:43.045500Z

@seancorfield what do you recommend to beginners then? Everyone I saw recommended luminus.

seancorfield 2021-07-27T02:29:47.046400Z

I usually recommend beginners start with Ring + ( Compojure | Reitit ) and understand that stuff first.

☝️ 1
1