Skip to content
Browse files

add stream handling (encode/decode)

  • Loading branch information...
1 parent 3a513a6 commit 1e21b7dd8ddbc8d4b94b948f47506ab4b5f51e53 @dakrone committed Mar 28, 2011
Showing with 94 additions and 1 deletion.
  1. +18 −0 src/clj/cheshire/core.clj
  2. +18 −1 test/cheshire/test/core.clj
  3. +58 −0 test/pass1.json
View
18 src/clj/cheshire/core.clj
@@ -26,6 +26,16 @@
(.flush generator)
(.toString sw)))
+(defn generate-stream
+ {:tag String
+ :doc "Returns a BufferedWriter for the given Clojure object with the.
+ JSON-encoded data written to the writer"}
+ [obj #^BufferedWriter writer & [date-format]]
+ (let [generator (.createJsonGenerator factory writer)]
+ (JsonExt/generate generator obj (or date-format default-date-format))
+ (.flush generator)
+ writer))
+
;; alias for clojure-json users
(def encode generate-string)
@@ -45,6 +55,14 @@
(.createJsonParser factory (StringReader. string))
true (or keywords false) nil))
+(defn parse-stream
+ "Returns the Clojure object corresponding to the given reader, reader must
+ implement BufferedReader."
+ [#^BufferedReader rdr & [keywords]]
+ (JsonExt/parse
+ (.createJsonParser factory rdr)
+ true (or keywords false) nil))
+
;; alias for clojure-json users
(def decode parse-string)
View
19 test/cheshire/test/core.clj
@@ -1,7 +1,8 @@
(ns cheshire.test.core
(:use clojure.test)
(:require [cheshire.core :as json])
- (:import (java.io StringReader BufferedReader)
+ (:import (java.io StringReader StringWriter
+ BufferedReader BufferedWriter)
(java.util Date UUID)))
(def test-obj {"int" 3 "long" 52001110638799097 "bigint" 9223372036854775808
@@ -70,3 +71,19 @@
(json/decode
(json/encode
{:foo id}))))))
+
+(deftest test-streams
+ (is (= {"foo" "bar"}
+ (json/parse-stream
+ (BufferedReader. (StringReader. "{\"foo\":\"bar\"}\n")))))
+ (let [sw (StringWriter.)
+ bw (BufferedWriter. sw)]
+ (json/generate-stream {"foo" "bar"} bw)
+ (is (= "{\"foo\":\"bar\"}" (.toString sw)))))
+
+(deftest test-jsondotorg-pass1
+ (let [string (slurp "test/pass1.json")
+ decoded-json (json/decode string)
+ encoded-json (json/encode decoded-json)
+ re-decoded-json (json/decode encoded-json)]
+ (is (= decoded-json re-decoded-json))))
View
58 test/pass1.json
@@ -0,0 +1,58 @@
+[
+ "JSON Test Pattern pass1",
+ {"object with 1 member":["array with 1 element"]},
+ {},
+ [],
+ -42,
+ true,
+ false,
+ null,
+ {
+ "integer": 1234567890,
+ "real": -9876.543210,
+ "e": 0.123456789e-12,
+ "E": 1.234567890E+34,
+ "": 23456789012E66,
+ "zero": 0,
+ "one": 1,
+ "space": " ",
+ "quote": "\"",
+ "backslash": "\\",
+ "controls": "\b\f\n\r\t",
+ "slash": "/ & \/",
+ "alpha": "abcdefghijklmnopqrstuvwyz",
+ "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
+ "digit": "0123456789",
+ "0123456789": "digit",
+ "special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
+ "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
+ "true": true,
+ "false": false,
+ "null": null,
+ "array":[ ],
+ "object":{ },
+ "address": "50 St. James Street",
+ "url": "http://www.JSON.org/",
+ "comment": "// /* <!-- --",
+ "# -- --> */": " ",
+ " s p a c e d " :[1,2 , 3
+
+,
+
+4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
+ "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
+ "quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
+ "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
+: "A key can be any string"
+ },
+ 0.5 ,98.6
+,
+99.44
+,
+
+1066,
+1e1,
+0.1e1,
+1e-1,
+1e00,2e+00,2e-00
+,"rosebud"]

0 comments on commit 1e21b7d

Please sign in to comment.
Something went wrong with that request. Please try again.