Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…king.
  • Loading branch information...
commit 55df9bbd266c11bf7c2627bf8cda6ef84f58e6dc 1 parent 2715781
Chris Perkins authored
2  project.clj
... ... @@ -1,4 +1,4 @@
1   -(defproject clj-msgpack "0.2.0"
  1 +(defproject clj-msgpack "0.3.0"
2 2 :description "Messagepack serialization library for Clojure."
3 3 :dependencies [
4 4 [org.clojure/clojure "1.4.0"]
10 src/clj_msgpack/core.clj
@@ -87,6 +87,10 @@
87 87 (unwrap [msgpack-obj]
88 88 "Unwrap one of the funky wrapper objects that msgpack uses."))
89 89
  90 +(def ^:dynamic *keywordize-strings*
  91 + "When true, unpack will convert strings that start with a colon into keywords."
  92 + false)
  93 +
90 94 (extend-protocol Unwrapable
91 95 ;; Specialized unwraps
92 96 BigIntegerValueImpl
@@ -110,7 +114,11 @@
110 114 NilValue
111 115 (unwrap [o] nil)
112 116 RawValue
113   - (unwrap [o] (.getString o)))
  117 + (unwrap [o]
  118 + (let [v (.getString o)]
  119 + (if (and *keywordize-strings* (.startsWith v ":"))
  120 + (keyword (.substring v 1))
  121 + v))))
114 122
115 123 (defn unpack [from]
116 124 (let [is (io/input-stream from) ; hmmm, can't use with-open here...
6 test/clj_msgpack/test/core.clj
@@ -41,6 +41,12 @@
41 41 ;(prn (round-trip obj))
42 42 (is (= obj (round-trip obj)))))
43 43
  44 +(deftest test-round-trip-keyword-default
  45 + (is (= ":foo" (round-trip :foo))))
  46 +
  47 +(deftest test-round-trip-keyword-with-keywordization
  48 + (is (= :foo (binding [*keywordize-strings* true] (round-trip :foo)))))
  49 +
44 50 (deftest test-pack-to-file
45 51 (let [data-in [{"yo" "dawg" 147 [true false]} "kldjfld" "kdajfkd" 37447]]
46 52 (with-open [f (output-stream "./temp.dat")]

0 comments on commit 55df9bb

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