This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-05-03
Channels
- # beginners (111)
- # boot (1)
- # braveandtrue (4)
- # calva (2)
- # cider (16)
- # clara (35)
- # cljdoc (4)
- # cljs-dev (22)
- # clojure (80)
- # clojure-dev (17)
- # clojure-europe (3)
- # clojure-italy (57)
- # clojure-japan (1)
- # clojure-nl (4)
- # clojure-serbia (1)
- # clojure-spec (25)
- # clojure-uk (108)
- # clojurescript (67)
- # cursive (17)
- # data-science (5)
- # datascript (6)
- # datomic (6)
- # devcards (1)
- # events (1)
- # expound (13)
- # figwheel (2)
- # figwheel-main (6)
- # fulcro (7)
- # jobs-discuss (8)
- # kaocha (1)
- # luminus (3)
- # nrepl (6)
- # off-topic (58)
- # re-frame (1)
- # reitit (16)
- # remote-jobs (1)
- # ring (1)
- # shadow-cljs (70)
- # spacemacs (10)
- # sql (42)
- # testing (1)
- # tools-deps (8)
- # vim (1)
Hi, I'm having trouble getting content-negotiation working in reitit using muuntaja. I can't write any java-time values as JSON. My understanding is that muuntaja hands off to jsonista, and that jsonista now supports java-time (as of 0.2.0, i'm using 0.2.2), but i'm thinking there's something i need to do to turn it on?
(j/write-value-as-string (jt/local-date-time))
Execution error (NoSuchMethodError) at com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase/createContextual (JSR310FormattedSerializerBase.java:123).
com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase.findFormatOverrides(Lcom/fasterxml/jackson/databind/SerializerProvider;Lcom/fasterxml/jackson/databind/BeanProperty;Ljava/lang/Class;)Lcom/fasterxml/jackson/annotation/JsonFormat$Value;
(import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)
(j/write-value-as-string
{:time (jt/local-date-time)}
(j/object-mapper
{:modules [(JavaTimeModule.)]}))
Execution error (NoSuchMethodError) at com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase/createContextual (JSR310FormattedSerializerBase.java:123).
com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase.findFormatOverrides(Lcom/fasterxml/jackson/databind/SerializerProvider;Lcom/fasterxml/jackson/databind/BeanProperty;Ljava/lang/Class;)Lcom/fasterxml/jackson/annotation/JsonFormat$Value;
This is me trying to get my head round it ^^^. In my route I've just defined a spec for the response and am letting the middleware do the content negotiation and coercion:
{:data {:coercion rcs/coercion
:muuntaja muuntaja/instance
:middleware [rrmm/format-middleware
rcoercion/coerce-exceptions-middleware
rcoercion/coerce-request-middleware
rcoercion/coerce-response-middleware]}
:exception pretty/exception}
Am I wrong in thinking that java-time can be written as JSON by jsonista/muuntaja? If so I'll ditch it (cheshire can't do it either), but obviously that's a reasonable piece of work so I'd rather avoid it.
Apologies if this shouldn't be in the #reitit channel, it's how i got here but perhaps not the root of the issue
I've worked around the issue by adding the following middleware outside the routing tree, but it's a shame as that feels like the wrong place for it (it walks every response and that's not necessary for things without dates in them)
(defn wrap-replace-dates
"Replaces java-time dates with strings"
[handler]
(fn [request]
(let [response (handler request)]
(update response :body #(walk/postwalk
(fn [node]
(if (instance? LocalDateTime node)
(str node)
node))
%)))))
An alternative would be to write a different set of specs for API responses that mirror the originals but replace all the dates with strings, but that's a lot of extra spec to maintain
@U053032QC. That's weird. the java.time should just work oob, see https://github.com/metosin/jsonista/blob/master/test/jsonista/core_test.clj#L56-L115
ok thanks, i'll see if i can put together a minimal repro next week. have a fun weekend
Just putting one together now. I also notice that coercion doesn't work for path parameters - they always come as a string, even if my spec says int?
. Maybe I've missed something that enables coercion?