nippy/README.md

124 lines
3.9 KiB
Markdown
Raw Normal View History

2012-07-06 12:53:02 +00:00
Current [semantic](http://semver.org/) version:
2012-07-01 17:13:32 +00:00
2012-07-06 12:53:02 +00:00
```clojure
2012-11-04 16:31:57 +00:00
[com.taoensso/nippy "1.0.0"]
2012-07-06 12:53:02 +00:00
```
2012-11-04 16:31:57 +00:00
# Nippy, a Clojure serialization library
2012-07-06 12:53:02 +00:00
Clojure's [rich data types](http://clojure.org/datatypes) are *awesome*. And its [reader](http://clojure.org/reader) allows you to take your data just about anywhere. But the reader can be painfully slow when you've got a lot of data to crunch (like when you're serializing to a database).
2012-07-06 19:12:59 +00:00
Nippy is an attempt to provide a drop-in, high-performance alternative to the reader. It's a fork of [Deep-Freeze](https://github.com/halgari/deep-freeze) and is used as the [Carmine Redis client](https://github.com/ptaoussanis/carmine) serializer.
2012-07-06 12:53:02 +00:00
## What's In The Box?
2012-11-04 16:31:57 +00:00
* Small, uncomplicated **all-Clojure** library.
* **Good performance**.
2012-07-06 12:53:02 +00:00
* Comprehesive, extensible **support for all major data types**.
2012-07-08 05:49:22 +00:00
* **Reader-fallback** for difficult/future types (including Clojure 1.4+ tagged literals).
2012-07-06 12:53:02 +00:00
* **Full test coverage** for every supported type.
* [Snappy](http://code.google.com/p/snappy/) **integrated de/compression** for efficient storage and network transfer.
## Getting Started
### Leiningen
Depend on Nippy in your `project.clj`:
```clojure
2012-11-04 16:31:57 +00:00
[com.taoensso/nippy "1.0.0"]
2012-07-06 12:53:02 +00:00
```
and `require` the library:
```clojure
(ns my-app (:require [taoensso.nippy :as nippy]))
```
### De/Serializing
2012-07-06 19:12:59 +00:00
As an example of what Nippy can do, let's take a look at its own reference stress data:
```clojure
nippy/stress-data
=>
2012-07-07 12:08:42 +00:00
{:bytes (byte-array [(byte 1) (byte 2) (byte 3)])
2012-07-06 19:12:59 +00:00
:nil nil
:boolean true
:char-utf8 \ಬ
:string-utf8 "ಬಾ ಇಲ್ಲಿ ಸಂಭವಿಸ"
:string-long (apply str (range 1000))
:keyword :keyword
2012-07-08 06:00:34 +00:00
:ns-keyword ::keyword
2012-07-06 19:12:59 +00:00
:list (list 1 2 3 4 5 (list 6 7 8 (list 9 10)))
:list-quoted '(1 2 3 4 5 (6 7 8 (9 10)))
:list-empty (list)
:vector [1 2 3 4 5 [6 7 8 [9 10]]]
:vector-empty []
:map {:a 1 :b 2 :c 3 :d {:e 4 :f {:g 5 :h 6 :i 7}}}
:map-empty {}
:set #{1 2 3 4 5 #{6 7 8 #{9 10}}}
:set-empty #{}
:meta (with-meta {:a :A} {:metakey :metaval})
:queue (-> (PersistentQueue/EMPTY) (conj :a :b :c :d :e :f :g))
:queue-empty (PersistentQueue/EMPTY)
:coll (repeatedly 1000 rand)
:byte (byte 16)
:short (short 42)
:integer (int 3)
:long (long 3)
:bigint (bigint 31415926535897932384626433832795)
:float (float 3.14)
:double (double 3.14)
:bigdec (bigdec 3.1415926535897932384626433832795)
2012-07-08 05:49:22 +00:00
:ratio 22/7
;; Clojure 1.4+
;; :tagged-uuid (java.util.UUID/randomUUID)
;; :tagged-date (java.util.Date.)
}
2012-07-06 19:12:59 +00:00
```
Serialize it:
```clojure
(def frozen-stress-data (nippy/freeze-to-bytes nippy/stress-data))
=> #<byte[] [B@3253bcf3>
```
Deserialize it:
```clojure
(nippy/thaw-from-bytes frozen-stress-data)
2012-07-07 12:08:42 +00:00
=> {:bytes (byte-array [(byte 1) (byte 2) (byte 3)])
2012-07-06 19:12:59 +00:00
:nil nil
:boolean true
<...> }
```
Couldn't be simpler!
2012-07-06 12:53:02 +00:00
## Performance
2012-07-06 19:12:59 +00:00
![Performance comparison chart](https://github.com/ptaoussanis/nippy/raw/master/benchmarks/chart1.png)
2012-07-06 12:53:02 +00:00
2012-07-06 19:12:59 +00:00
![Data size chart](https://github.com/ptaoussanis/nippy/raw/master/benchmarks/chart2.png)
2012-07-06 12:53:02 +00:00
[Detailed benchmark information](https://docs.google.com/spreadsheet/ccc?key=0AuSXb68FH4uhdE5kTTlocGZKSXppWG9sRzA5Y2pMVkE&pli=1#gid=0) is available on Google Docs.
## Nippy supports the ClojureWerkz Project Goals
ClojureWerkz is a growing collection of open-source, batteries-included [Clojure libraries](http://clojurewerkz.org/) that emphasise modern targets, great documentation, and thorough testing.
## Contact & Contribution
2012-11-04 16:31:57 +00:00
Reach me (Peter Taoussanis) at *ptaoussanis at gmail.com* for questions/comments/suggestions/whatever. I'm very open to ideas if you have any! I'm also on Twitter: [@ptaoussanis](https://twitter.com/#!/ptaoussanis).
2012-07-06 12:53:02 +00:00
## License
2012-11-04 16:31:57 +00:00
Copyright &copy; 2012 Peter Taoussanis. Distributed under the [Eclipse Public License](http://www.eclipse.org/legal/epl-v10.html), the same as Clojure.