github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

danlarkin / clojure-json

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 52
    • 7
  • Source
  • Commits
  • Network (7)
  • Issues (0)
  • Downloads (0)
  • Wiki (2)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (3)
    • master ✓
    • protocols
    • writer
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A JSON encoder/parser for clojure — Read more

  cancel

http://blog.danlarkin.org/projects/clojure-json/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

updated README to reflect switch to leiningen 
danlarkin (author)
Thu Nov 19 21:06:02 -0800 2009
commit  fa674adf621dff5af5367bbb3b3f253c984096fa
tree    798c33c539533a281d586b5db51cf9bc9b21bcac
parent  7178b745964604effc29a5d7763b664c20129455
clojure-json /
name age
history
message
file .gitignore Thu Nov 19 20:29:51 -0800 2009 remove build.xml and pom.xml -- leiningen is no... [danlarkin]
file LICENSE Sat Nov 01 20:57:35 -0700 2008 Initial Commit [danlarkin]
file README.markdown Thu Nov 19 21:06:02 -0800 2009 updated README to reflect switch to leiningen [danlarkin]
file project.clj Thu Nov 19 20:21:10 -0800 2009 added project.clj for leiningen compatibility [danlarkin]
directory src/ Sun Nov 01 09:09:02 -0800 2009 handle when hashmap keys are bignums [danlarkin]
directory test/ Thu Nov 19 20:21:10 -0800 2009 added project.clj for leiningen compatibility [danlarkin]
README.markdown

Installing

clojure-json is on clojars! http://clojars.org/org.danlarkin/clojure-json

To install from source you can lein install but likely you can just add this project as a dependency like it says on http://clojars.org/org.danlarkin/clojure-json.

Using The Encoder

user=> (require '(org.danlarkin [json :as json]))
nil
user=> (print (json/encode-to-str [1 2 3 4 5]))
[1,2,3,4,5]nil
user=> (print (json/encode-to-str {:a 1 :b 2 :c 3}))
{"a":1,"b":2,"c":3}nil
user=> (print (json/encode-to-str [1 2 3 4 5] :indent 2))
[
  1,
  2,
  3,
  4,
  5
]nil
user=> (import '(java.io FileWriter))
nil
user=> (json/encode-to-writer [1 2 3 4 5] (FileWriter. "/tmp/foo.json"))
#<FileWriter java.io.FileWriter@c93f91>
user=> (import '(java.util Date))
nil
user=> (json/encode-to-str (Date.))
java.lang.Exception: Unknown Datastructure: Sat Nov 5 19:00:00 GMT 1605 (NO_SOURCE_FILE:0)
user=> (defn date-encoder
        [date writer pad current-indent start-token-indent indent-size]
        (.append writer (str start-token-indent \" date \")))
#'user/date-encoder
user=> (json/add-encoder java.util.Date date-encoder)
#<MultiFn clojure.lang.MultiFn@da6c0d>
user=> (print (json/encode-to-str (Date.)))
"Sat Nov 5 19:00:00 GMT 1605"nil
user=> (print (json/encode-to-str [(Date.) (Date.) (Date.)] :indent 2))
[
  "Sat Nov 5 19:00:00 GMT 1605",
  "Sat Nov 5 19:00:00 GMT 1605",
  "Sat Nov 5 19:00:00 GMT 1605"
]nil
user=> (print (json/encode-to-str {:foo (Date.) :bam 4 :quux 'bar} :indent 2))
{
  "foo":"Sat Nov 5 19:00:00 GMT 1605",
  "bam":4,
  "quux":"bar"
}nil

Custom Encoding

clojure-json uses a multimethod for custom encoding, dispatching on type. If you're adding an encoder function for a container type make sure to respect all of the indentation arguments that your function will be passed and to call encode-helper on each of the elements in your container. I've left the parameters to date-encoder un-hinted in this example in the interest of brevity but their inclusion does seem to speed up execution time a good bit so I suggest using them where speed matters.

Using The Parser

user=> (require '(org.danlarkin [json :as json]))
nil
user=> (json/decode-from-str "[1, 2, 3, 4, 5]")
[1 2 3 4 5]
user=> (json/decode-from-str "{\"foo\":1, \"bar\":2, \"baz\":3}")
{:foo 1, :bar 2, :baz 3}
user=> (json/decode-from-str "{\"foo\":[1,2,\"superbam\"], \"bar\":{\"bam\":98.6}, \"baz\":3}")
{:foo [1 2 "superbam"], :bar {:bam 98.6}, :baz 3}
user=> (json/decode-from-reader (FileReader. "/tmp/foo.json"))
[1 2 3 4 5]

Special Thanks

Special thanks go to Darrell Bishop of arubanetworks.com for writing the parser included in this distribution.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server