Permalink
Browse files

Added an option to keywordize strings with a leading colon when unpac…

…king.
  • Loading branch information...
1 parent 2715781 commit 55df9bbd266c11bf7c2627bf8cda6ef84f58e6dc Chris Perkins committed Oct 17, 2012
Showing with 16 additions and 2 deletions.
  1. +1 −1 project.clj
  2. +9 −1 src/clj_msgpack/core.clj
  3. +6 −0 test/clj_msgpack/test/core.clj
View
2 project.clj
@@ -1,4 +1,4 @@
-(defproject clj-msgpack "0.2.0"
+(defproject clj-msgpack "0.3.0"
:description "Messagepack serialization library for Clojure."
:dependencies [
[org.clojure/clojure "1.4.0"]
View
10 src/clj_msgpack/core.clj
@@ -87,6 +87,10 @@
(unwrap [msgpack-obj]
"Unwrap one of the funky wrapper objects that msgpack uses."))
+(def ^:dynamic *keywordize-strings*
+ "When true, unpack will convert strings that start with a colon into keywords."
+ false)
+
(extend-protocol Unwrapable
;; Specialized unwraps
BigIntegerValueImpl
@@ -110,7 +114,11 @@
NilValue
(unwrap [o] nil)
RawValue
- (unwrap [o] (.getString o)))
+ (unwrap [o]
+ (let [v (.getString o)]
+ (if (and *keywordize-strings* (.startsWith v ":"))
+ (keyword (.substring v 1))
+ v))))
(defn unpack [from]
(let [is (io/input-stream from) ; hmmm, can't use with-open here...
View
6 test/clj_msgpack/test/core.clj
@@ -41,6 +41,12 @@
;(prn (round-trip obj))
(is (= obj (round-trip obj)))))
+(deftest test-round-trip-keyword-default
+ (is (= ":foo" (round-trip :foo))))
+
+(deftest test-round-trip-keyword-with-keywordization
+ (is (= :foo (binding [*keywordize-strings* true] (round-trip :foo)))))
+
(deftest test-pack-to-file
(let [data-in [{"yo" "dawg" 147 [true false]} "kldjfld" "kdajfkd" 37447]]
(with-open [f (output-stream "./temp.dat")]

0 comments on commit 55df9bb

Please sign in to comment.