diff --git a/.gitignore b/.gitignore index 6eae6a5..00ac6a4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ pom.xml *jar lib classes +*.dat diff --git a/README.md b/README.md new file mode 100644 index 0000000..aefc7da --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# clj-msgpack # + +This is a Clojure wrapper for the [MessagePack](http://msgpack.org/) +library. + +It allows you serialize and deserialize simple clojure objects to and +from a well-defined binary format that is portable to many other +programming languages. + +## Example ## + +Put some objects in a file: + + (require '[clj-msgpack.core :as mp]) + (use '[clojure.java.io :only [output-stream]]) + + (def data [nil true false {"yo" "dawg"} ["foo" "bar"]]) + (with-open [f (output-stream "./temp.dat")] + (mp/pack-into f "Hello" 23) + (apply mp/pack-into f data)) + + +Pull them back out somewhere else (eg: ruby): + + require 'msgpack' + u = MessagePack::Unpacker.new + u.feed( File.read('./temp.dat') ) + u.each {|ob| p ob} + "Hello" + 23 + nil + true + false + {"yo"=>"dawg"} + ["foo", "bar"] + + +## Why? ## + +Why would you want to use this instead of, say, JSON, or Java +serialization? Honestly, I'm not sure yet :) The serialization format +is portable to many languages, and it's quite compact, especially if +you are serializing a lot of numbers. For stringy data, it's probably +not much smaller than JSON. At some point I'll do some benchmarks to +see how small and/or fast this is compared to some of the +alternatives. + diff --git a/test/clj_msgpack/test/core.clj b/test/clj_msgpack/test/core.clj index 0610249..428da28 100644 --- a/test/clj_msgpack/test/core.clj +++ b/test/clj_msgpack/test/core.clj @@ -1,6 +1,7 @@ (ns clj-msgpack.test.core (:use [clj-msgpack.core] :reload) - (:use [clojure.test])) + (:use [clojure.test]) + (:use [clojure.java.io :only [input-stream output-stream]])) (defn- round-trip [obj] (let [ba (pack obj)] @@ -33,7 +34,17 @@ {23 "a" 44 "b"} {23 ["x" "y"] 44 {"a" -7}} {23 ["x" "y"] nil {"a" -7}} + ; lists come back as vectors, but still compare as equal + (list "foo" 28838272 (list {"blah" "bloo"} nil)) ]] - (prn obj) - (prn (round-trip obj)) + ;(prn obj) + ;(prn (round-trip obj)) (is (= obj (round-trip obj))))) + +(deftest test-pack-to-file + (let [data-in [{"yo" "dawg" 147 [true false]} "kldjfld" "kdajfkd" 37447]] + (with-open [f (output-stream "./temp.dat")] + (apply pack-into f data-in)) + (let [data-out (unpack "./temp.dat")] + (is (= data-in data-out))))) +