Permalink
Browse files

Add README. Add test for pack-into.

  • Loading branch information...
1 parent fe180dd commit 23cf83cf9395b6a2df9ebf0f575d9c5c674586b7 @grammati committed Mar 1, 2011
Showing with 62 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +47 −0 README.md
  3. +14 −3 test/clj_msgpack/test/core.clj
View
@@ -2,3 +2,4 @@ pom.xml
*jar
lib
classes
+*.dat
View
@@ -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.
+
@@ -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)))))
+

0 comments on commit 23cf83c

Please sign in to comment.