-
Notifications
You must be signed in to change notification settings - Fork 47
/
json.clj
60 lines (41 loc) · 1.48 KB
/
json.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(ns com.brunobonacci.mulog.common.json
(:require [jsonista.core :as json]
[com.brunobonacci.mulog.utils :as ut])
(:import com.fasterxml.jackson.core.json.WriterBasedJsonGenerator))
;;
;; Add encoders for various types
;;
(def encoders
(atom
{;; Add Exception encoder to JSON generator
java.lang.Throwable
(fn [x ^WriterBasedJsonGenerator gen]
(.writeString gen ^String (ut/exception-stacktrace x)))
;; Add Flake encoder to JSON generator
com.brunobonacci.mulog.core.Flake
(fn [x ^WriterBasedJsonGenerator gen]
(.writeString gen ^String (str x)))}))
(def default-mapper-options
{:date-format "yyyy-MM-dd'T'HH:mm:ss.SSSX"})
(def ^:private mapper-options
(memoize
(fn [pretty? encoders]
(json/object-mapper
(assoc default-mapper-options
:pretty pretty?
:encoders encoders)))))
(def pretty-mapper
(json/object-mapper
(assoc default-mapper-options :pretty? true)))
(defn to-json
"It takes a map and return a JSON encoded string of the given map data."
([m]
(json/write-value-as-string m (mapper-options false @encoders)))
([m {:keys [pretty?]}]
(json/write-value-as-string m (mapper-options pretty? @encoders))))
(defn from-json
"Parses a JSON encoded string `s` into the representing data"
([s]
(json/read-value s json/keyword-keys-object-mapper))
([s {:keys [keywordize]}]
(json/read-value s (if keywordize json/keyword-keys-object-mapper json/default-object-mapper))))