Skip to content

Commit

Permalink
Add README. Add test for pack-into.
Browse files Browse the repository at this point in the history
  • Loading branch information
grammati committed Mar 1, 2011
1 parent fe180dd commit 23cf83c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ pom.xml
*jar
lib
classes
*.dat
47 changes: 47 additions & 0 deletions 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.

17 changes: 14 additions & 3 deletions 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)]
Expand Down Expand Up @@ -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.