-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathjson.clj
More file actions
481 lines (402 loc) · 15.7 KB
/
Copy pathjson.clj
File metadata and controls
481 lines (402 loc) · 15.7 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
;; Copyright (c) Stuart Sierra, 2012. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
(ns ^{:author "Stuart Sierra"
:doc "JavaScript Object Notation (JSON) parser/generator.
See http://www.json.org/"}
clojure.data.json
(:refer-clojure :exclude (read))
(:require [clojure.pprint :as pprint])
(:import (java.io PrintWriter PushbackReader StringWriter
Writer StringReader EOFException)))
;;; JSON READER
(def ^{:dynamic true :private true} *bigdec*)
(def ^{:dynamic true :private true} *key-fn*)
(def ^{:dynamic true :private true} *value-fn*)
(defn- default-write-key-fn
[x]
(cond (instance? clojure.lang.Named x)
(name x)
(nil? x)
(throw (Exception. "JSON object properties may not be nil"))
:else (str x)))
(defn- default-value-fn [k v] v)
(declare -read)
(defmacro ^:private codepoint [c]
(int c))
(defn- codepoint-clause [[test result]]
(cond (list? test)
[(map int test) result]
(= test :whitespace)
['(9 10 13 32) result]
(= test :simple-ascii)
[(remove #{(codepoint \") (codepoint \\) (codepoint \/)}
(range 32 127))
result]
:else
[(int test) result]))
(defmacro ^:private codepoint-case [e & clauses]
`(case ~e
~@(mapcat codepoint-clause (partition 2 clauses))
~@(when (odd? (count clauses))
[(last clauses)])))
(defn- read-array [^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the
;; opening bracket.
(loop [result (transient [])]
(let [c (.read stream)]
(when (neg? c)
(throw (EOFException. "JSON error (end-of-file inside array)")))
(codepoint-case c
:whitespace (recur result)
\, (recur result)
\] (persistent! result)
(do (.unread stream c)
(let [element (-read stream true nil)]
(recur (conj! result element))))))))
(defn- read-object [^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the
;; opening bracket.
(loop [key nil, result (transient {})]
(let [c (.read stream)]
(when (neg? c)
(throw (EOFException. "JSON error (end-of-file inside object)")))
(codepoint-case c
:whitespace (recur key result)
\, (recur nil result)
\: (recur key result)
\} (if (nil? key)
(persistent! result)
(throw (Exception. "JSON error (key missing value in object)")))
(do (.unread stream c)
(let [element (-read stream true nil)]
(if (nil? key)
(if (string? element)
(recur element result)
(throw (Exception. "JSON error (non-string key in object)")))
(recur nil
(let [out-key (*key-fn* key)
out-value (*value-fn* out-key element)]
(if (= *value-fn* out-value)
result
(assoc! result out-key out-value)))))))))))
(defn- read-hex-char [^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the
;; initial "\u". Reads the next four characters from the stream.
(let [a (.read stream)
b (.read stream)
c (.read stream)
d (.read stream)]
(when (or (neg? a) (neg? b) (neg? c) (neg? d))
(throw (EOFException.
"JSON error (end-of-file inside Unicode character escape)")))
(let [s (str (char a) (char b) (char c) (char d))]
(char (Integer/parseInt s 16)))))
(defn- read-escaped-char [^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the
;; initial backslash.
(let [c (.read stream)]
(codepoint-case c
(\" \\ \/) (char c)
\b \backspace
\f \formfeed
\n \newline
\r \return
\t \tab
\u (read-hex-char stream))))
(defn- read-quoted-string [^PushbackReader stream]
;; Expects to be called with the head of the stream AFTER the
;; opening quotation mark.
(let [buffer (StringBuilder.)]
(loop []
(let [c (.read stream)]
(when (neg? c)
(throw (EOFException. "JSON error (end-of-file inside string)")))
(codepoint-case c
\" (str buffer)
\\ (do (.append buffer (read-escaped-char stream))
(recur))
(do (.append buffer (char c))
(recur)))))))
(defn- read-integer [^String string]
(if (< (count string) 18) ; definitely fits in a Long
(Long/valueOf string)
(or (try (Long/valueOf string)
(catch NumberFormatException e nil))
(bigint string))))
(defn- read-decimal [^String string]
(if *bigdec*
(bigdec string)
(Double/valueOf string)))
(defn- read-number [^PushbackReader stream]
(let [buffer (StringBuilder.)
decimal? (loop [decimal? false]
(let [c (.read stream)]
(codepoint-case c
(\- \+ \0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
(do (.append buffer (char c))
(recur decimal?))
(\e \E \.)
(do (.append buffer (char c))
(recur true))
(do (.unread stream c)
decimal?))))]
(if decimal?
(read-decimal (str buffer))
(read-integer (str buffer)))))
(defn- -read
[^PushbackReader stream eof-error? eof-value]
(loop []
(let [c (.read stream)]
(if (neg? c) ;; Handle end-of-stream
(if eof-error?
(throw (EOFException. "JSON error (end-of-file)"))
eof-value)
(codepoint-case
c
:whitespace (recur)
;; Read numbers
(\- \0 \1 \2 \3 \4 \5 \6 \7 \8 \9)
(do (.unread stream c)
(read-number stream))
;; Read strings
\" (read-quoted-string stream)
;; Read null as nil
\n (if (and (= (codepoint \u) (.read stream))
(= (codepoint \l) (.read stream))
(= (codepoint \l) (.read stream)))
nil
(throw (Exception. "JSON error (expected null)")))
;; Read true
\t (if (and (= (codepoint \r) (.read stream))
(= (codepoint \u) (.read stream))
(= (codepoint \e) (.read stream)))
true
(throw (Exception. "JSON error (expected true)")))
;; Read false
\f (if (and (= (codepoint \a) (.read stream))
(= (codepoint \l) (.read stream))
(= (codepoint \s) (.read stream))
(= (codepoint \e) (.read stream)))
false
(throw (Exception. "JSON error (expected false)")))
;; Read JSON objects
\{ (read-object stream)
;; Read JSON arrays
\[ (read-array stream)
(throw (Exception.
(str "JSON error (unexpected character): " (char c)))))))))
(defn read
"Reads a single item of JSON data from a java.io.Reader. Options are
key-value pairs, valid options are:
:eof-error? boolean
If true (default) will throw exception if the stream is empty.
:eof-value Object
Object to return if the stream is empty and eof-error? is
false. Default is nil.
:bigdec boolean
If true use BigDecimal for decimal numbers instead of Double.
Default is false.
:key-fn function
Single-argument function called on JSON property names; return
value will replace the property names in the output. Default
is clojure.core/identity, use clojure.core/keyword to get
keyword properties.
:value-fn function
Function to transform values in the output. For each JSON
property, value-fn is called with two arguments: the property
name (transformed by key-fn) and the value. The return value
of value-fn will replace the value in the output. If value-fn
returns itself, the property will be omitted from the output.
The default value-fn returns the value unchanged."
[reader & options]
(let [{:keys [eof-error? eof-value bigdec key-fn value-fn]
:or {bigdec false
eof-error? true
key-fn identity
value-fn default-value-fn}} options]
(binding [*bigdec* bigdec
*key-fn* key-fn
*value-fn* value-fn]
(-read (PushbackReader. reader) eof-error? eof-value))))
(defn read-str
"Reads one JSON value from input String. Options are the same as for
read."
[string & options]
(apply read (StringReader. string) options))
;;; JSON WRITER
(def ^{:dynamic true :private true} *escape-unicode*)
(def ^{:dynamic true :private true} *escape-slash*)
(defprotocol JSONWriter
(-write [object out]
"Print object to PrintWriter out as JSON"))
(defn- write-string [^CharSequence s ^PrintWriter out]
(let [sb (StringBuilder. (count s))]
(.append sb \")
(dotimes [i (count s)]
(let [cp (int (.charAt s i))]
(codepoint-case cp
;; Printable JSON escapes
\" (.append sb "\\\"")
\\ (.append sb "\\\\")
\/ (.append sb (if *escape-slash* "\\/" "/"))
;; Simple ASCII characters
:simple-ascii (.append sb (.charAt s i))
;; JSON escapes
\backspace (.append sb "\\b")
\formfeed (.append sb "\\f")
\newline (.append sb "\\n")
\return (.append sb "\\r")
\tab (.append sb "\\t")
;; Any other character is Unicode
(if *escape-unicode*
(.append sb (format "\\u%04x" cp)) ; Hexadecimal-escaped
(.appendCodePoint sb cp)))))
(.append sb \")
(.print out (str sb))))
(defn- write-object [m ^PrintWriter out]
(.print out \{)
(loop [x m]
(when (seq m)
(let [[k v] (first x)
out-key (*key-fn* k)
out-value (*value-fn* k v)
nxt (next x)]
(when-not (string? out-key)
(throw (Exception. "JSON object keys must be strings")))
(when-not (= *value-fn* out-value)
(write-string out-key out)
(.print out \:)
(-write out-value out)
(when (seq nxt)
(.print out \,)))
(when (seq nxt)
(recur nxt)))))
(.print out \}))
(defn- write-array [s ^PrintWriter out]
(.print out \[)
(loop [x s]
(when (seq x)
(let [fst (first x)
nxt (next x)]
(-write fst out)
(when (seq nxt)
(.print out \,)
(recur nxt)))))
(.print out \]))
(defn- write-bignum [x ^PrintWriter out]
(.print out (str x)))
(defn- write-plain [x ^PrintWriter out]
(.print out x))
(defn- write-null [x ^PrintWriter out]
(.print out "null"))
(defn- write-named [x out]
(write-string (name x) out))
(defn- write-generic [x out]
(if (.isArray (class x))
(-write (seq x) out)
(throw (Exception. (str "Don't know how to write JSON of " (class x))))))
(defn- write-ratio [x out]
(-write (double x) out))
;; nil, true, false
(extend nil JSONWriter {:-write write-null})
(extend java.lang.Boolean JSONWriter {:-write write-plain})
;; Numbers
(extend java.lang.Number JSONWriter {:-write write-plain})
(extend clojure.lang.Ratio JSONWriter {:-write write-ratio})
(extend java.math.BigInteger JSONWriter {:-write write-bignum})
(extend java.math.BigDecimal JSONWriter {:-write write-bignum})
;; Attempt to support Clojure 1.2.x:
(when-let [class (try (.. Thread currentThread getContextClassLoader
(loadClass "clojure.lang.BigInt"))
(catch ClassNotFoundException _ false))]
(extend class JSONWriter {:-write write-bignum}))
;; Symbols, Keywords, and Strings
(extend clojure.lang.Named JSONWriter {:-write write-named})
(extend java.lang.CharSequence JSONWriter {:-write write-string})
;; Collections
(extend java.util.Map JSONWriter {:-write write-object})
(extend java.util.Collection JSONWriter {:-write write-array})
;; Maybe a Java array, otherwise fail
(extend java.lang.Object JSONWriter {:-write write-generic})
(defn write
"Write JSON-formatted output to a java.io.Writer. Options are
key-value pairs, valid options are:
:escape-unicode boolean
If true (default) non-ASCII characters are escaped as \\uXXXX
:escape-slash boolean
If true (default) the slash / is escaped as \\/
:key-fn function
Single-argument function called on map keys; return value will
replace the property names in the output. Must return a
string. Default calls clojure.core/name on symbols and
keywords and clojure.core/str on everything else.
:value-fn function
Function to transform values before writing. For each
key-value pair in the input, called with two arguments: the
key (BEFORE transformation by key-fn) and the value. The
return value of value-fn will replace the value in the output.
If the return value is a number, boolean, string, or nil it
will be included literally in the output. If the return value
is a non-map collection, it will be processed recursively. If
the return value is a map, it will be processed recursively,
calling value-fn again on its key-value pairs. If value-fn
returns itself, the key-value pair will be omitted from the
output."
[x ^Writer writer & options]
(let [{:keys [escape-unicode escape-slash key-fn value-fn]
:or {escape-unicode true
escape-slash true
key-fn default-write-key-fn
value-fn default-value-fn}} options]
(binding [*escape-unicode* escape-unicode
*escape-slash* escape-slash
*key-fn* key-fn
*value-fn* value-fn]
(-write x (PrintWriter. writer)))))
(defn write-str
"Converts x to a JSON-formatted string. Options are the same as
write."
[x & options]
(let [sw (StringWriter.)]
(apply write x sw options)
(.toString sw)))
;;; JSON PRETTY-PRINTER
;; Based on code by Tom Faulhaber
(defn- pprint-array [s]
((pprint/formatter-out "~<[~;~@{~w~^, ~:_~}~;]~:>") s))
(defn- pprint-object [m]
((pprint/formatter-out "~<{~;~@{~<~w:~_~w~:>~^, ~_~}~;}~:>")
(for [[k v] m] [(*key-fn* k) v])))
(defn- pprint-generic [x]
(if (.isArray (class x))
(pprint-array (seq x))
;; pprint proxies Writer, so we can't just wrap it
(print (with-out-str (-write x (PrintWriter. *out*))))))
(defn- pprint-dispatch [x]
(cond (nil? x) (print "null")
(instance? java.util.Map x) (pprint-object x)
(instance? java.util.Collection x) (pprint-array x)
(instance? clojure.lang.ISeq x) (pprint-array x)
:else (pprint-generic x)))
(defn pprint
"Pretty-prints JSON representation of x to *out*. Options are the
same as for write except :value-fn, which is not supported."
[x & options]
(let [{:keys [escape-unicode escape-slash key-fn]
:or {escape-unicode true
escape-slash true
key-fn default-write-key-fn}} options]
(binding [*escape-unicode* escape-unicode
*escape-slash* escape-slash
*key-fn* key-fn]
(pprint/write x :dispatch pprint-dispatch))))
(load "json_compat_0_1")
;; Local Variables:
;; mode: clojure
;; eval: (define-clojure-indent (codepoint-case (quote defun)))
;; End: