https://github.com/EuAndreh/cl-bson.git
git clone 'https://github.com/EuAndreh/cl-bson.git'
(ql:quickload :cl-bson)
Inspired by cl-mongo and mongo-cl-driver.
API and documentation generated by the (awesome) Codex.
* (ql:quickload :cl-bson)
; => (:CL-BSON)
* (in-package :cl-bson)
; => #<PACKAGE "CL-BSON">
* (named-readtables:in-readtable bson-syntax)
; => big alist of readtables...
* (enable-printers)
; => NIL
;; Obs.: all read macros are just optional ;)
The main functions are encode
and decode
:
```lisp
* (decode (encode (make-document)))
; ⇒ #d(“_id” #i(057DCB554D4143C13A5FBB91))
* (encode #d(:a-key 'a-value))
; ⇒ #(24 0 0 0 2 65 45 75 69 89 0 7 0 0 0 65 45 86 65 76 85 69 0 0)
* (decode *)
; ⇒ #d(“A-KEY” “A-VALUE”)
(encode #d(:keyword-key :keyword-value
‘symbol-key ‘symbol-value
“string-key” “string-value”
“will be coerced to double” 1.5
“will stay as double” 1.5d0
“embedded document” #d(“one more level” #d(“i believe” “that's enough”))
“one more” (let ((doc (make-document)))
(add-element doc “document” “example”))
“sequence” #(1 2 3)
“vector” #(1 2 3)
“list” ’(1 2 3)
“regex” #/\d+\/$/li
“binary data” (make-instance ’
(decode *)
; ⇒ #d(“KEYWORD-KEY” “KEYWORD-VALUE”
“SYMBOL-KEY” “SYMBOL-VALUE”
“string-key” “string-value”
“will be coerced to double” 1.5d0
“will stay as double” 1.5d0
“embedded document” #d(“one more level” #d(“i believe” “that's enough”))
“one more” #d(“document” “example”)
“sequence” #(1 2 3)
“vector” #(1 2 3)
“list” #(1 2 3)
“regex” #/\d+\/$/il
“binary data” #<
Examples from the FAQ: ```lisp ;; Original: {“hello”: “world”} * (encode #d(“hello” “world”)) ; ⇒ #(22 0 0 0 2 104 101 108 108 111 0 5 0 0 0 119 111 114 108 100 0 0)
;; Since the example is in hexadecimal base: * (map 'vector (lambda (_) (let ((print-base 16)) (princ-to-string _))) *)
;; Output:
“2” “68” “65” “6C” “6C” “6F” “0” “6” “0” “0” “0” “77” “6F” “72” “6C” “64” “0” “0”)
\x16\x00\x00\x00 // total document size \x02 // 0x02 = type String hello\x00 // field name \x06\x00\x00\x00world\x00 // field value \x00 // 0x00 = type EOO (‘end of object’)
;; Original: {“BSON”: [“awesome”, 5.05, 1986]} * (encode #d(“BSON” #(“awesome” 5.05d0 1986)))
;; To hexadecimal: * (map 'vector (lambda (_) (let ((print-base 16)) (princ-to-string _))) *)
;; Output:
“4” “42” “53” “4F” “4E” “0” “26” “0” “0” “0” “2” “30” “0” “8” “0” “0” “0” “61” “77” “65” “73” “6F” “6D” “65” “0” “1” “31” “0” “33” “33” “33” “33” “33” “33” “14” “40” “10” “32” “0” “C2” “7” “0” “0” “0” “0”)
\x31\x00\x00\x00 \x04BSON\x00 \x26\x00\x00\x00 \x02\x30\x00\x08\x00\x00\x00awesome\x00 \x01\x31\x00\x33\x33\x33\x33\x33\x33\x14\x40 \x10\x32\x00\xc2\x07\x00\x00 \x00 \x00 ```
Check the detailed explanation of the last example.
This library depends on: + arrow-macros + babel + cl-intbytes + fast-io + ieee-floats + let-over-lambda + local-time + named-readtables + rutils + trivial-shell
The test package uses the prove test library, and the documentation uses the Codex documentation library.
(ql:quickload :cl-bson)
If you find any bug or inconsistency in the code, or if you find it too hard to use, please, feel free to open an issue.
This library is tested under SBCL and CCL Common Lisp implementations.
To run all the defined tests, use:
lisp
* (asdf:test-system :cl-bson)
; prints lots of (colorful) stuff...
; => T
Tests are ran with Travis CI and Circle CI using cl-travis, CIM, cl-coveralls and Roswell. Check it out!