metosin / jsonista

Clojure library for fast JSON encoding and decoding.
https://cljdoc.org/d/metosin/jsonista
Eclipse Public License 2.0
422 stars 30 forks source link

How to use the modules option to decode unix epoch #25

Closed ronnac closed 5 years ago

ronnac commented 5 years ago

Could you please add an example for how to use the :modules parameter to decode (read-value) a time from unix epoch milliseconds to human readable or instant?

miikka commented 5 years ago

Do you mean like you have JSON like this:

{"timestamp": 1573127103425}

And then you'd like to get Clojure data like this:

{:timestamp #inst "2019-11-07T11:45:03.425Z"}

If yes, I'm not sure if jsonista is really the right tool for the job. You'd need to know which numbers you want to convert to instants and which ones should be kept as-is. Jsonista does not easily support decoding object members based on the keys. It's probably technically possible to do using modules, but I'd recommend using jsonista to parse json and then coercing the value using some other library.

ronnac commented 5 years ago

Ok. I wrote myself a conversion function based on the tick library. I'll decide on a case by case basis which values to convert with it.

ikitommi commented 5 years ago

You could do this as a separate step usin malli & custom transformers can help to to derive the required transformation or spec-tools & coercion.