Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit e60ce738665a67ae6e95c5c2873fb51d65337b74 @danlarkin danlarkin committed Nov 2, 2008
Showing with 134 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +24 −0 LICENSE
  3. +20 −0 README.markdown
  4. +89 −0 src/org/danlarkin/json/json.clj
@@ -0,0 +1 @@
+*~
24 LICENSE
@@ -0,0 +1,24 @@
+;; Copyright (c) 2008 Dan Larkin
+;; All rights reserved.
+
+;; Redistribution and use in source and binary forms, with or without
+;; modification, are permitted provided that the following conditions
+;; are met:
+;; 1. Redistributions of source code must retain the above copyright
+;; notice, this list of conditions and the following disclaimer.
+;; 2. Redistributions in binary form must reproduce the above copyright
+;; notice, this list of conditions and the following disclaimer in the
+;; documentation and/or other materials provided with the distribution.
+;; 3. The name of the author may not be used to endorse or promote products
+;; derived from this software without specific prior written permission.
+
+;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,20 @@
+# Installing #
+
+* Add the src directory to your classpath.
+* Reference it with something like
+ `(ns foo (:require (org.danlarkin [json :as json])))`
+
+# Using #
+
+ foo=> (json/encode [1,2,3,4,5])
+ "[1,2,3,4,5]"
+ foo=> (json/encode {"a" 1 "b" 2 "c" 3})
+ "{\"a\":1,\"b\":2,\"c\":3}"
+
+# Note #
+
+This package can't decode json currently, only encode.
+There is, however, a decoder available on the clojure
+mailing list here:
+http://groups.google.com/group/clojure/browse_thread/thread/c25325debf4c0d78
+Hopefully soon I'll add decoding capability.
@@ -0,0 +1,89 @@
+;; Copyright (c) 2008 Dan Larkin
+;; All rights reserved.
+
+;; Redistribution and use in source and binary forms, with or without
+;; modification, are permitted provided that the following conditions
+;; are met:
+;; 1. Redistributions of source code must retain the above copyright
+;; notice, this list of conditions and the following disclaimer.
+;; 2. Redistributions in binary form must reproduce the above copyright
+;; notice, this list of conditions and the following disclaimer in the
+;; documentation and/or other materials provided with the distribution.
+;; 3. The name of the author may not be used to endorse or promote products
+;; derived from this software without specific prior written permission.
+
+;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+;; IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+(ns org.danlarkin.json
+ (:require (clojure.contrib [str-utils :as str-utils])))
+
+(def encode) ;encode is used before it's defined, so we have to
+ ;pre-define it
+
+(defn- create-hash-pairs
+ "Helper function for encoding maps.
+ Returns a vector of string key:value pairs"
+ [hmap]
+ (loop [dict hmap
+ accumulator []]
+ (if (= (count dict)
+ 0)
+ accumulator
+ (let [k (ffirst dict)
+ v (second (first dict))]
+ (recur (rest dict)
+ (conj accumulator
+ (str (encode k)
+ ":"
+ (encode v))))))))
+
+(defn- encode-map
+ [hmap & opts]
+ (let [opts (apply hash-set opts)
+ pad (if (:pad opts) \newline "")
+ indent (if (:pad opts) " " "")]
+ (str \{
+ pad
+ (str-utils/str-join (str "," pad)
+ (for [x (create-hash-pairs hmap)]
+ (str indent x)))
+ pad
+ \})))
+
+(defn- encode-coll
+ [lst & opts]
+ (let [opts (apply hash-set opts)
+ pad (if (:pad opts) \newline "")
+ indent (if (:pad opts) " " "")]
+ (str \[
+ pad
+ (str-utils/str-join (str "," pad)
+ (for [x lst]
+ (str indent (encode x))))
+ pad
+ \])))
+
+(defn encode
+ "This is the only function exported from this namespace.
+ It takes an arbitrarily nested clojure datastructure
+ and returns a JSON-encoded string representation."
+ [value & opts]
+ (cond
+ (= (class value) java.lang.Boolean) value
+ (nil? value) 'null
+ (string? value) (str \" value \")
+ (number? value) value
+ (symbol? value) (str \" value \")
+ (keyword? value) (str \" value \")
+ (map? value) (apply encode-map value opts)
+ (coll? value) (apply encode-coll value opts)
+ :else (throw (Exception. "Unknown Datastructure"))))

0 comments on commit e60ce73

Please sign in to comment.