Mercurial > lasercutter
view src/clojure/contrib/test_contrib/pprint/examples/json.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;;; json.clj: A pretty printing version of the JavaScript Object Notation (JSON) generator3 ;; by Tom Faulhaber, based on the version by Stuart Sierra (clojure.contrib.json.write)4 ;; May 9, 20096 ;; Copyright (c) Tom Faulhaber/Stuart Sierra, 2009. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)9 ;; which can be found in the file epl-v10.html at the root of this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.15 (ns16 #^{:author "Tom Faulhaber (based on the version by Stuart Sierra)",17 :doc "Pretty printing JavaScript Object Notation (JSON) generator.19 This is an example of using a pretty printer dispatch function to generate JSON output",20 :see-also [["http://json.org/", "JSON Home Page"]]}21 clojure.contrib.pprint.examples.json22 (:use [clojure.test :only (deftest- is)]23 [clojure.contrib.string :only (as-str)]24 [clojure.contrib.pprint :only (write formatter-out)]))28 (defmulti dispatch-json29 "The dispatch function for printing objects as JSON"30 {:arglists '[[x]]}31 (fn [x] (cond32 (nil? x) nil ;; prevent NullPointerException on next line33 (.isArray (class x)) ::array34 :else (type x))))36 ;; Primitive types can be printed with Clojure's pr function.37 (derive java.lang.Boolean ::pr)38 (derive java.lang.Byte ::pr)39 (derive java.lang.Short ::pr)40 (derive java.lang.Integer ::pr)41 (derive java.lang.Long ::pr)42 (derive java.lang.Float ::pr)43 (derive java.lang.Double ::pr)45 ;; Collection types can be printed as JSON objects or arrays.46 (derive java.util.Map ::object)47 (derive java.util.Collection ::array)49 ;; Symbols and keywords are converted to strings.50 (derive clojure.lang.Symbol ::symbol)51 (derive clojure.lang.Keyword ::symbol)54 (defmethod dispatch-json ::pr [x] (pr x))56 (defmethod dispatch-json nil [x] (print "null"))58 (defmethod dispatch-json ::symbol [x] (pr (name x)))60 (defmethod dispatch-json ::array [s]61 ((formatter-out "~<[~;~@{~w~^, ~:_~}~;]~:>") s))63 (defmethod dispatch-json ::object [m]64 ((formatter-out "~<{~;~@{~<~w:~_~w~:>~^, ~_~}~;}~:>")65 (for [[k v] m] [(as-str k) v])))67 (defmethod dispatch-json java.lang.CharSequence [s]68 (print \")69 (dotimes [i (count s)]70 (let [cp (Character/codePointAt s i)]71 (cond72 ;; Handle printable JSON escapes before ASCII73 (= cp 34) (print "\\\"")74 (= cp 92) (print "\\\\")75 ;; Print simple ASCII characters76 (< 31 cp 127) (print (.charAt s i))77 ;; Handle non-printable JSON escapes78 (= cp 8) (print "\\b")79 (= cp 12) (print "\\f")80 (= cp 10) (print "\\n")81 (= cp 13) (print "\\r")82 (= cp 9) (print "\\t")83 ;; Any other character is printed as Hexadecimal escape84 :else (printf "\\u%04x" cp))))85 (print \"))87 (defn print-json88 "Prints x as JSON. Nil becomes JSON null. Keywords become89 strings, without the leading colon. Maps become JSON objects, all90 other collection types become JSON arrays. Java arrays become JSON91 arrays. Unicode characters in strings are escaped as \\uXXXX.92 Numbers print as with pr."93 [x]94 (write x :dispatch dispatch-json))96 (defn json-str97 "Converts x to a JSON-formatted string."98 [x]99 (with-out-str (print-json x)))103 ;;; TESTS105 ;; Run these tests with106 ;; (clojure.test/run-tests 'clojure.contrib.print-json)108 ;; Bind clojure.test/*load-tests* to false to omit these109 ;; tests from production code.111 (deftest- can-print-json-strings112 (is (= "\"Hello, World!\"" (json-str "Hello, World!")))113 (is (= "\"\\\"Embedded\\\" Quotes\"" (json-str "\"Embedded\" Quotes"))))115 (deftest- can-print-unicode116 (is (= "\"\\u1234\\u4567\"" (json-str "\u1234\u4567"))))118 (deftest- can-print-json-null119 (is (= "null" (json-str nil))))121 (deftest- can-print-json-arrays122 (is (= "[1, 2, 3]" (json-str [1 2 3])))123 (is (= "[1, 2, 3]" (json-str (list 1 2 3))))124 (is (= "[1, 2, 3]" (json-str (sorted-set 1 2 3))))125 (is (= "[1, 2, 3]" (json-str (seq [1 2 3])))))127 (deftest- can-print-java-arrays128 (is (= "[1, 2, 3]" (json-str (into-array [1 2 3])))))130 (deftest- can-print-empty-arrays131 (is (= "[]" (json-str [])))132 (is (= "[]" (json-str (list))))133 (is (= "[]" (json-str #{}))))135 (deftest- can-print-json-objects136 (is (= "{\"a\":1, \"b\":2}" (json-str (sorted-map :a 1 :b 2)))))138 (deftest- object-keys-must-be-strings139 (is (= "{\"1\":1, \"2\":2}" (json-str (sorted-map 1 1 2 2)))))141 (deftest- can-print-empty-objects142 (is (= "{}" (json-str {}))))