Permalink
Browse files

MsgPack 0.6.6 support. Pretty big change.

Also split the namespace.
  • Loading branch information...
1 parent da2138f commit ec46ef8d18fdd938fecfd22f6dd419fff628851e @KirinDave committed Jun 18, 2012
Showing with 120 additions and 114 deletions.
  1. +1 −1 project.clj
  2. +0 −111 src/clj_msgpack/core.clj
  3. +74 −0 src/clj_msgpack/pack.clj
  4. +44 −0 src/clj_msgpack/unpack.clj
  5. +1 −2 test/clj_msgpack/test/core.clj
View
@@ -2,7 +2,7 @@
:description "Messagepack serialization library for Clojure."
:dependencies [
[org.clojure/clojure "1.4.0"]
- [org.msgpack/msgpack "0.5.2-devel"]
+ [org.msgpack/msgpack "0.6.6"]
]
:repositories {"msgpack" "http://msgpack.org/maven2/"}
)
@@ -1,111 +0,0 @@
-(ns clj-msgpack.core
- (:import (org.msgpack Packer Unpacker)
- (org.msgpack.object ArrayType BigIntegerTypeIMPL BooleanType
- DoubleTypeIMPL FloatTypeIMPL IntegerType
- LongIntegerTypeIMPL MapType NilType
- RawType ShortIntegerTypeIMPL))
- (:use (clojure.java [io :only [input-stream output-stream]])))
-
-(defprotocol Packable
- "Serialize the object to the packer."
- (pack-me [obj packer] "Serialize the object into the packer."))
-
-(extend-protocol Packable
-
- nil
- (pack-me [_ ^Packer packer]
- (.packNil packer))
-
- clojure.lang.Keyword
- (pack-me [kw ^Packer packer]
- (.pack packer (str \: (name kw)))) ; not round-trippable, but
- ; better than nothing.
-
- clojure.lang.Symbol
- (pack-me [sym ^Packer packer]
- (.pack packer (name sym)))
-
- clojure.lang.Sequential
- (pack-me [s ^Packer packer]
- (.packArray packer (count s))
- (doseq [item s]
- (pack-me item packer)))
-
- clojure.lang.IPersistentMap
- (pack-me [m ^Packer packer]
- (.packMap packer (count m))
- (doseq [[k v] m]
- (pack-me k packer)
- (pack-me v packer)))
-
- Object
- (pack-me [obj ^Packer packer]
- (.pack packer obj))
-
- )
-
-(defprotocol ToPacker
- (to-packer [obj]))
-
-(extend-protocol ToPacker
- ;Convert an object into an org.msgpack.Packer instance.
- Packer
- (to-packer [p] p)
- Object
- (to-packer [obj]
- (-> obj output-stream Packer.))
- )
-
-(defn packer [dest]
- (to-packer dest))
-
-(defn pack-into
- "Pack objects to the destination, which must be a Packer or coercible to an InputStream."
- [dest & objs]
- (let [p (packer dest)]
- (doseq [obj objs]
- (pack-me obj p))
- p))
-
-(defn pack
- "Pack the objects into a byte array and return it."
- [& objs]
- (let [ba (java.io.ByteArrayOutputStream.)
- p (Packer. ba)]
- (apply pack-into p objs)
- (.toByteArray ba)))
-
-
-(defprotocol Unwrapable
- (unwrap [msgpack-obj]
- "Unwrap one of the funky wrapper objects that msgpack uses."))
-
-(extend-protocol Unwrapable
- ArrayType
- (unwrap [o] (into [] (map unwrap (.asArray o))))
- BigIntegerTypeIMPL
- (unwrap [o] (.asBigInteger o))
- BooleanType
- (unwrap [o] (.asBoolean o))
- DoubleTypeIMPL
- (unwrap [o] (.asDouble o))
- FloatTypeIMPL
- (unwrap [o] (.asFloat o))
- IntegerType
- (unwrap [o] (.asInt o))
- LongIntegerTypeIMPL
- (unwrap [o] (.asLong o))
- MapType
- (unwrap [o] (into {} (map (fn [[k v]] [(unwrap k) (unwrap v)]) (.asMap o))))
- NilType
- (unwrap [o] nil)
- RawType
- (unwrap [o] (.asString o))
- ShortIntegerTypeIMPL
- (unwrap [o] (.asInt o))
- )
-
-(defn unpack [from]
- (let [is (input-stream from) ; hmmm, can't use with-open here...
- u (Unpacker. is)]
- (map unwrap u)))
@@ -0,0 +1,74 @@
+(ns clj-msgpack.pack
+ (:require [clojure.java.io :as io])
+ (:import org.msgpack.packer.Packer)
+ (:import org.msgpack.MessagePack))
+
+(defprotocol Packable
+ "Serialize the object to the packer."
+ (pack-me [obj packer] "Serialize the object into the packer."))
+
+(extend-protocol Packable
+ nil
+ (pack-me [_ ^Packer packer]
+ (.writeNil packer))
+
+ clojure.lang.Keyword
+ (pack-me [kw ^Packer packer]
+ (.write packer ^String (str \: (name kw)))) ; not round-trippable, but
+ ; better than nothing.
+
+ clojure.lang.Symbol
+ (pack-me [sym ^Packer packer]
+ (.write packer ^String (name sym)))
+
+ clojure.lang.Sequential
+ (pack-me [s ^Packer packer]
+ (.writeArrayBegin packer (count s))
+ (doseq [item s]
+ (pack-me item packer))
+ (.writeArrayEnd packer))
+
+ clojure.lang.IPersistentMap
+ (pack-me [m ^Packer packer]
+ (.writeMapBegin packer (count m))
+ (doseq [[k v] m]
+ (pack-me k packer)
+ (pack-me v packer))
+ (.writeMapEnd packer))
+
+ Object
+ (pack-me [obj ^Packer packer]
+ (.write packer ^Object obj)))
+
+
+(defprotocol ToPacker
+ (to-packer [obj]))
+
+(extend-protocol ToPacker
+ ;Convert an object into an org.msgpack.Packer instance.
+ Packer
+ (to-packer [p] p)
+
+ Object
+ (to-packer [obj]
+ (let [mpacker (MessagePack.)]
+ (.createPacker mpacker (io/output-stream obj)))))
+
+
+(defn packer [dest]
+ (to-packer dest))
+
+(defn pack-into
+ "Pack objects to the destination, which must be a Packer or coercible to an InputStream."
+ [dest & objs]
+ (let [p (packer dest)]
+ (doseq [obj objs]
+ (pack-me obj p))
+ p))
+
+(defn pack
+ "Pack the objects into a byte array and return it."
+ [& objs]
+ (let [p (.createBufferPacker (MessagePack.))]
+ (apply pack-into p objs)
+ (.toByteArray p)))
@@ -0,0 +1,44 @@
+(ns clj-msgpack.unpack
+ (:use (clojure.java (io :only (input-stream))))
+ (:import org.msgpack.unpacker.Unpacker)
+ (:import org.msgpack.MessagePack)
+ (:import (org.msgpack.type ArrayValue BooleanValue MapValue RawValue Value
+ FloatValue IntegerValue NilValue NumberValue
+ ;; These are internal and are probably a source of cross-version
+ ;; headaches.
+ BigIntegerValueImpl IntValueImpl LongValueImpl
+ DoubleValueImpl FloatValueImpl)))
+
+(defprotocol Unwrapable
+ (unwrap [msgpack-obj]
+ "Unwrap one of the funky wrapper objects that msgpack uses."))
+
+(extend-protocol Unwrapable
+ ;; Specialized unwraps
+ BigIntegerValueImpl
+ (unwrap [o] (.getBigInteger o))
+ DoubleValueImpl
+ (unwrap [o] (.getDouble o))
+ FloatValueImpl
+ (unwrap [o] (.getFloat o))
+ LongValueImpl
+ (unwrap [o] (.getLong o))
+
+ ;; Non-specialized
+ IntegerValue
+ (unwrap [o] (.getInt o))
+ ArrayValue
+ (unwrap [o] (into [] (map unwrap (.getElementArray o))))
+ BooleanValue
+ (unwrap [o] (.getBoolean o))
+ MapValue
+ (unwrap [o] (into {} (map (fn [[k v]] [(unwrap k) (unwrap v)]) o)))
+ NilValue
+ (unwrap [o] nil)
+ RawValue
+ (unwrap [o] (.getString o)))
+
+(defn unpack [from]
+ (let [is (input-stream from) ; hmmm, can't use with-open here...
+ u (.createUnpacker (MessagePack.) is)]
+ (map unwrap u)))
@@ -1,5 +1,5 @@
(ns clj-msgpack.test.core
- (:use [clj-msgpack.core] :reload)
+ (:use [clj-msgpack pack unpack] :reload)
(:use [clojure.test])
(:use [clojure.java.io :only [input-stream output-stream]]))
@@ -47,4 +47,3 @@
(apply pack-into f data-in))
(let [data-out (unpack "./temp.dat")]
(is (= data-in data-out)))))
-

0 comments on commit ec46ef8

Please sign in to comment.