dakrone / cheshire

Clojure JSON and JSON SMILE (binary json format) encoding/decoding
https://github.com/dakrone/cheshire
MIT License
1.49k stars 152 forks source link

Custom conversion when parsing? #171

Open ejstembler opened 3 years ago

ejstembler commented 3 years ago

I have code which parses JSON; all keyword values are parsed as strings. Later, I map through the maps and overwrite one of the keywords with the date version of its string data. I have it broken out into two functions and it works. I'm wonding if there's a way to just have the parse function do this? In actuality I have two other elements which need to be converted to dates as well.

(ns clojure-json-test.core
  (:gen-class)
  (:require [clj-time.core :as t]
            [clj-time.format :as f]
            [clj-time.local :as l]
            [cheshire.core :refer :all]
            [org.httpkit.client :as client])
  (:import [java.net URI]
           [javax.net.ssl SNIHostName SSLEngine SSLParameters]))

;; other code snipped

(defn get-posts-body 
  []
  (:body @(client/get posts-url api-options)))

(defn get-posts
  []
  (parse-string (get-posts-body) true))

(defn get-posts-with-dates
  []
  (map #(merge % {:last_viewed_at (f/parse (f/formatters :date-time-no-ms) (:last_viewed_at %))}) (get-posts)))