Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/danlarkin/clojure-json
Browse files Browse the repository at this point in the history
* 'master' of git://github.com/danlarkin/clojure-json:
  fixed bug where passing in an empty string to the decoder would cause an exception... now it just returns an empty string
  changed custom encoder to dispatch on clojure.core/type instead of clojure.core/class
  fix test suite to run with new test-is reporting syntax
  • Loading branch information
dysinger committed May 5, 2009
2 parents e6a8f6c + 2a337ea commit a5bcfef
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/org/danlarkin/json.clj
Expand Up @@ -44,9 +44,9 @@
so I recommend using them.
"
[type f]
[type-dispatcher f]
`(let [args# (gensym "args")]
(defmethod encoder/encode-custom ~type
(defmethod encoder/encode-custom ~type-dispatcher
[& args#]
(apply ~f args#))))

Expand Down
7 changes: 5 additions & 2 deletions src/org/danlarkin/json/decoder.clj
Expand Up @@ -182,15 +182,18 @@
NOTE: decode-value is not responsible for eating whitespace after the value."
[#^BufferedReader b-reader]
(let [_ (.mark b-reader 1)
char (char (.read b-reader))]
int-char (.read b-reader)
char (char int-char)]
(cond
(= char \{) (decode-object b-reader)
(= char \[) (decode-array b-reader)
(= char \") (decode-string b-reader)
(= char \f) (decode-const b-reader "alse" false)
(= char \t) (decode-const b-reader "rue" true)
(= char \n) (decode-const b-reader "ull" nil)
:else (decode-number b-reader))))
:else (if (= -1 int-char)
""
(decode-number b-reader)))))

(defn decode-from-buffered-reader
[#^BufferedReader reader]
Expand Down
2 changes: 1 addition & 1 deletion src/org/danlarkin/json/encoder.clj
Expand Up @@ -125,7 +125,7 @@
(defmulti encode-custom
;Multimethod for encoding classes of objects that
;aren't handled by the default encode-helper.
(fn [value & _] (class value)))
(fn [value & _] (type value)))

(defmethod
#^{:private true}
Expand Down
18 changes: 13 additions & 5 deletions test/test.clj
Expand Up @@ -11,10 +11,14 @@
decoded-string# (json/decode-from-str json-string#)
result# (= json-form# decoded-string#)]
(if result#
(report :pass ~msg '~form `(~'~'= ~json-form# ~decoded-string#))
(report :fail ~msg
`(~'~'= ~json-form# ~decoded-string#)
(list '~'not= json-form# decoded-string#)))
(report {:type :pass
:message ~msg
:expected '~form
:actual `(~'~'= ~json-form# ~decoded-string#)})
(report {:type :fail
:message ~msg
:expected `(~'~'= ~json-form# ~decoded-string#)
:actual (list '~'not= json-form# decoded-string#)}))
result#))


Expand Down Expand Up @@ -43,6 +47,10 @@
(deftest array-of-objects
(is (:json= [{:foo 1} {:bar 2}])))

(deftest empty-string
(is (= (json/decode-from-str "")
"")))

;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Numbers ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -111,7 +119,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Indenting ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftest array-of-objects
(deftest indenting
(is (= (json/encode-to-str [{:foo 1},{:bar 2}] :indent 2)
"[\n {\n \"foo\":1\n },\n {\n \"bar\":2\n }\n]")))

Expand Down

0 comments on commit a5bcfef

Please sign in to comment.