-
Notifications
You must be signed in to change notification settings - Fork 29
/
core.clj
262 lines (211 loc) · 9.92 KB
/
core.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
(ns jsonista.core
"JSON encoding and decoding based on Jackson Databind.
Encoding example:
(require '[jsonista.core :as json])
(json/write-value-as-string {:hello 1})
;; => \"{\\\"hello\\\":1}\"
Decoding example:
(def +data+ (json/write-value-as-string {:foo \"bar\"}))
(json/read-value +data+)
;; => {\"foo\" \"bar\"}
## Configuration
You can configure encoding and decoding by creating a custom mapper object
with jsonista.core/object-mapper. The options are passed in as a map.
For example, to convert map keys into keywords while decoding:
(json/read-value +data+ (json/object-mapper {:decode-key-fn true}))
;; => {:foo \"bar\"}
See the docstring of [[object-mapper]] for all available options.
## Custom encoders
Custom encoder is a function that take a value and a JsonGenerator object as
the parameters. The function should call JsonGenerator methods to emit the
desired JSON. This is the same as how custom encoders work in Cheshire.
Custom encoders are configured by the object-mapper option :encoders, which is a
map from types to encoder functions.
For example, to encode java.awt.Color:
(let [encoders {java.awt.Color (fn [color gen] (.writeString gen (str color)))}
mapper (json/object-mapper {:encoders encoders})]
(json/write-value-as-string (java.awt.Color. 1 2 3) mapper))
;; => \"\\\"java.awt.Color[r=1,g=2,b=3]\\\"\"
## Jsonista vs. Cheshire
jsonista uses Jackson Databind while Cheshire uses Jackson Core. In our
benchmarks, jsonista performs better than Cheshire (take look at
json_perf_test.clj). On the other hand, Cheshire has a wider set of features
and has been used in production much more."
(:import
(jsonista.jackson
DateSerializer
FunctionalKeyDeserializer
FunctionalSerializer
KeywordSerializer
KeywordKeyDeserializer
PersistentHashMapDeserializer
PersistentVectorDeserializer
SymbolSerializer
RatioSerializer FunctionalKeywordSerializer)
(com.fasterxml.jackson.core JsonGenerator$Feature
JsonFactory)
(com.fasterxml.jackson.databind
JsonSerializer
ObjectMapper
module.SimpleModule
SerializationFeature DeserializationFeature)
(com.fasterxml.jackson.databind.module SimpleModule)
(java.io InputStream Writer File OutputStream DataOutput Reader)
(java.net URL)
(com.fasterxml.jackson.datatype.jsr310 JavaTimeModule)))
(defn- clojure-module
"Create a Jackson Databind module to support Clojure datastructures.
See [[object-mapper]] docstring for the documentation of the options."
[{:keys [encode-key-fn decode-key-fn encoders date-format]
:or {encode-key-fn true, decode-key-fn false}}]
(doto (SimpleModule. "Clojure")
(.addDeserializer java.util.List (PersistentVectorDeserializer.))
(.addDeserializer java.util.Map (PersistentHashMapDeserializer.))
(.addSerializer clojure.lang.Keyword (KeywordSerializer. false))
(.addSerializer clojure.lang.Ratio (RatioSerializer.))
(.addSerializer clojure.lang.Symbol (SymbolSerializer.))
(.addSerializer java.util.Date (if date-format
(DateSerializer. date-format)
(DateSerializer.)))
(as-> module
(doseq [[type encoder] encoders]
(cond
(instance? JsonSerializer encoder) (.addSerializer module type encoder)
(fn? encoder) (.addSerializer module type (FunctionalSerializer. encoder))
:else (throw (ex-info
(str "Can't register encoder " encoder " for type " type)
{:type type, :encoder encoder})))))
(cond->
(true? decode-key-fn) (.addKeyDeserializer Object (KeywordKeyDeserializer.))
(fn? decode-key-fn) (.addKeyDeserializer Object (FunctionalKeyDeserializer. decode-key-fn))
(true? encode-key-fn) (.addKeySerializer clojure.lang.Keyword (KeywordSerializer. true))
(fn? encode-key-fn) (.addKeySerializer clojure.lang.Keyword (FunctionalKeywordSerializer. encode-key-fn)))))
(defn ^ObjectMapper object-mapper
"Create an ObjectMapper with Clojure support.
The optional first parameter is a map of options. The following options are
available:
| Mapper options | |
| ------------------- | ---------------------------------------------------------- |
| `:modules` | vector of extra ObjectMapper modules |
| `:factory` | A Jackson JsonFactory for this given mapper |
| `:mapper` | The base ObjectMapper to start with - overrides `:factory` |
| Encoding options | |
| ------------------- | ------------------------------------------------- |
| `:pretty` | set to true use Jacksons pretty-printing defaults |
| `:escape-non-ascii` | set to true to escape non ascii characters |
| `:date-format` | string for custom date formatting. default: `yyyy-MM-dd'T'HH:mm:ss'Z'` |
| `:encode-key-fn` | true to coerce keyword keys to strings, false to leave them as keywords, or a function to provide custom coercion (default: true) |
| `:encoders` | a map of custom encoders where keys should be types and values should be encoder functions |
Encoder functions take two parameters: the value to be encoded and a
JsonGenerator object. The function should call JsonGenerator methods to emit
the desired JSON.
| Decoding options | |
| ------------------- | -------------------------------------------------------------- |
| `:decode-key-fn` | true to coerce keys to keywords, false to leave them as strings, or a function to provide custom coercion (default: false) |
| `:bigdecimals` | true to decode doubles as BigDecimals (default: false) |"
([] (object-mapper {}))
([options]
(let [factory (:factory options)
maybe-mapper (:mapper options)
base-mapper (cond
maybe-mapper maybe-mapper
factory (ObjectMapper. ^JsonFactory factory)
:else (ObjectMapper.))
mapper (doto ^ObjectMapper base-mapper
(.registerModule (JavaTimeModule.))
(.registerModule (clojure-module options))
(cond->
(:pretty options) (.enable SerializationFeature/INDENT_OUTPUT)
(:bigdecimals options) (.enable DeserializationFeature/USE_BIG_DECIMAL_FOR_FLOATS)
(:escape-non-ascii options) (doto (-> .getFactory (.enable JsonGenerator$Feature/ESCAPE_NON_ASCII)))))]
(doseq [module (:modules options)]
(.registerModule mapper module))
(.disable mapper SerializationFeature/WRITE_DATES_AS_TIMESTAMPS)
mapper)))
(def ^:deprecated ^ObjectMapper +default-mapper+
"DEPRECATED: The default ObjectMapper instance."
(object-mapper {}))
(def ^ObjectMapper default-object-mapper
"The default ObjectMapper instance."
(object-mapper {}))
(def ^ObjectMapper keyword-keys-object-mapper
"ObjectMapper instance that uses keyword keys for maps"
(object-mapper {:encode-key-fn true, :decode-key-fn true}))
;;
;; Protocols
;;
(defprotocol ReadValue
(-read-value [this mapper]))
(extend-protocol ReadValue
(Class/forName "[B")
(-read-value [this ^ObjectMapper mapper]
(.readValue mapper ^bytes this ^Class Object))
nil
(-read-value [_ _])
File
(-read-value [this ^ObjectMapper mapper]
(.readValue mapper this ^Class Object))
URL
(-read-value [this ^ObjectMapper mapper]
(.readValue mapper this ^Class Object))
String
(-read-value [this ^ObjectMapper mapper]
(.readValue mapper this ^Class Object))
Reader
(-read-value [this ^ObjectMapper mapper]
(.readValue mapper this ^Class Object))
InputStream
(-read-value [this ^ObjectMapper mapper]
(.readValue mapper this ^Class Object)))
(defprotocol WriteValue
(-write-value [this value mapper]))
(extend-protocol WriteValue
File
(-write-value [this value ^ObjectMapper mapper]
(.writeValue mapper this value))
OutputStream
(-write-value [this value ^ObjectMapper mapper]
(.writeValue mapper this value))
DataOutput
(-write-value [this value ^ObjectMapper mapper]
(.writeValue mapper this value))
Writer
(-write-value [this value ^ObjectMapper mapper]
(.writeValue mapper this value)))
;;
;; public api
;;
(defn read-value
"Decodes a value from a JSON from anything that
satisfies [[ReadValue]] protocol. By default,
File, URL, String, Reader and InputStream are supported.
To configure, pass in an ObjectMapper created with [[object-mapper]],
see [[object-mapper]] docstring for the available options."
([object]
(-read-value object default-object-mapper))
([object ^ObjectMapper mapper]
(-read-value object mapper)))
(defn ^String write-value-as-string
"Encode a value as a JSON string.
To configure, pass in an ObjectMapper created with [[object-mapper]]."
([object]
(.writeValueAsString default-object-mapper object))
([object ^ObjectMapper mapper]
(.writeValueAsString mapper object)))
(defn write-value-as-bytes
"Encode a value as a JSON byte-array.
To configure, pass in an ObjectMapper created with [[object-mapper]]."
{:tag 'bytes}
([object]
(.writeValueAsBytes default-object-mapper object))
([object ^ObjectMapper mapper]
(.writeValueAsBytes mapper object)))
(defn write-value
"Encode a value as JSON and write using the provided [[WriteValue]] instance.
By default, File, OutputStream, DataOutput and Writer are supported.
To configure, pass in an ObjectMapper created with [[object-mapper]],
see [[object-mapper]] docstring for the available options."
([to object]
(-write-value to object default-object-mapper))
([to object ^ObjectMapper mapper]
(-write-value to object mapper)))