aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/json/read.clj
blob: 8b4f69c0a5319de8689c7688a851f4f564accd44 (plain)
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
;;; json/read.clj: JavaScript Object Notation (JSON) parser

;; by Stuart Sierra, http://stuartsierra.com/
;; January 26, 2009

;; Copyright (c) Stuart Sierra, 2009. 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.



;; For more information on JSON, see http://www.json.org/



(ns clojure.contrib.json.read
  (:import (java.io PushbackReader StringReader EOFException))
  (:use [clojure.contrib.test-is :only (deftest is)]))

(declare read-json)

(defn- read-json-array [#^PushbackReader stream]
  ;; Expects to be called with the head of the stream AFTER the
  ;; opening bracket.
  (loop [i (.read stream), result []]
    (let [c (char i)]
      (cond
       (= i -1) (throw (EOFException. "JSON error (end-of-file inside array)"))
       (Character/isWhitespace c) (recur (.read stream) result)
       (= c \,) (recur (.read stream) result)
       (= c \]) result
       :else (do (.unread stream (int c))
                 (let [element (read-json stream)]
                   (recur (.read stream) (conj result element))))))))

(defn- read-json-object [#^PushbackReader stream]
  ;; Expects to be called with the head of the stream AFTER the
  ;; opening bracket.
  (loop [i (.read stream), key nil, result {}]
    (let [c (char i)]
      (cond
       (= i -1) (throw (EOFException. "JSON error (end-of-file inside object)"))

       (Character/isWhitespace c) (recur (.read stream) key result)

       (= c \,) (recur (.read stream) nil result)

       (= c \:) (recur (.read stream) key result)

       (= c \}) (if (nil? key)
                  result
                  (throw (Exception. "JSON error (key missing value in object)")))

       :else (do (.unread stream i)
                 (let [element (read-json stream)]
                   (if (nil? key)
                     (if (string? element)
                       (recur (.read stream) element result)
                       (throw (Exception. "JSON error (non-string key in object)")))
                     (recur (.read stream) nil (assoc result key element)))))))))

(defn read-json
  "Read the next JSON record from stream, which must be an instance of
  java.io.PushbackReader."
  ([] (read-json *in* true nil))
  ([stream] (read-json stream true nil))
  ([#^PushbackReader stream eof-error? eof-value]
     (loop [i (.read stream)]
       (let [c (char i)]
         (cond
          ;; Handle end-of-stream
          (= i -1) (if eof-error?
                     (throw (EOFException. "JSON error (end-of-file)"))
                     eof-value)

          ;; Ignore whitespace
          (Character/isWhitespace c) (recur (.read stream))

          ;; Read strings, numbers, true, and false with Clojure reader
          (#{\" \- \0 \1 \2 \3 \4 \5 \6 \7 \8 \9} c)
          (do (.unread stream i)
              (read stream true nil))

          ;; Read null as nil
          (= c \n) (let [ull [(char (.read stream))
                              (char (.read stream))
                              (char (.read stream))]]
                     (if (= ull [\u \l \l])
                       nil
                       (throw (Exception. (str "JSON error (expected null): " c ull)))))

          ;; Read true
          (= c \t) (let [rue [(char (.read stream))
                              (char (.read stream))
                              (char (.read stream))]]
                     (if (= rue [\r \u \e])
                       true
                       (throw (Exception. (str "JSON error (expected true): " c rue)))))

          ;; Read false
          (= c \f) (let [alse [(char (.read stream))
                               (char (.read stream))
                               (char (.read stream))
                               (char (.read stream))]]
                     (if (= alse [\a \l \s \e])
                       false
                       (throw (Exception. (str "JSON error (expected false): " c alse)))))

          

          ;; Read JSON objects
          (= c \{) (read-json-object stream)

          ;; Read JSON arrays
          (= c \[) (read-json-array stream)

          :else (throw (Exception. (str "JSON error (unexpected character): " c))))))))


(defn read-json-string [string]
  (read-json (PushbackReader. (StringReader. string))))


;;; TESTS

(deftest can-read-numbers
  (is (= 42 (read-json-string "42")))
  (is (= -3 (read-json-string "-3")))
  (is (= 3.14159 (read-json-string "3.14159")))
  (is (= 6.022e23 (read-json-string "6.022e23"))))

(deftest can-read-null
  (is (= nil (read-json-string "null"))))

(deftest can-read-strings
  (is (= "Hello, World!" (read-json-string "\"Hello, World!\""))))

(deftest can-read-booleans
  (is (= true (read-json-string "true")))
  (is (= false (read-json-string "false"))))

(deftest can-ignore-whitespace
  (is (= nil (read-json-string "\r\n   null"))))

(deftest can-read-arrays
  (is (= [1 2 3] (read-json-string "[1,2,3]")))
  (is (= ["Ole" "Lena"] (read-json-string "[\"Ole\", \r\n \"Lena\"]"))))

(deftest can-read-objects
  (is (= {"a" 1, "b" 2} (read-json-string "{\"a\": 1, \"b\": 2}"))))

(deftest can-read-nested-structures
  (is (= {"a" [1 2 {"b" [3 "four"]} 5.5]}
         (read-json-string "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}"))))

(deftest disallows-non-string-keys
  (is (thrown? Exception (read-json-string "{26:\"z\""))))

(deftest disallows-barewords
  (is (thrown? Exception (read-json-string "  foo  "))))

(deftest disallows-unclosed-arrays
  (is (thrown? Exception (read-json-string "[1, 2,  "))))

(deftest disallows-unclosed-objects
  (is (thrown? Exception (read-json-string "{\"a\":1,  "))))