"Instead of cursing the darkness, light a candle." ― Benjamin Franklin
A functional Clojure(script) BEncode serialization library.
[org.jamesleonis/bencode-cljc "0.2.0"]
bencode-cljc
aims to be a portable implementation of the BEncode spec, to boldly go wherever Clojure goes.
(ns your.awesome.app
(:require [bencode-cljc.core :refer [serialize deserialize]]))
(def out (serialize {"spam" (list "a" "b")})
; => "d4:spaml1:a1:bee"
(deserialize out)
; => {"spam" '("a" "b")}
serialize
operates on Clojure data structures and outputs a Clojure string.
deserialize
operates on the opposite of serialize
.
If an error is encountered in either, nil
is returned.
As an encoder, bencode-cljc
is fairly strict. The aim is full compatibility with existing BEncoded services, and not a fully featured transport encoder. As such, only Integer, Maps, Lists, and Strings are supported.
This comes with some unfortunate side-effects:
nil
values are unsupported.
nil
/null
values, so they are treated as a malformed input.float
, decimal
, and other floating-point numbers are not allowedmap
keys are coerced to strings. Complex values (lists, maps) and numbers are not allowed as keys.But I'm not an evil taskmaster. I bend.
name
. This truncates the namespace from namespaced keywords.
In keeping with a portable implementation, deserialize
operates on vanilla Clojure strings.
Contrary to the Wikipedia entry, BEncode does not have the bijection property. List item order is undefined [1] [2]. Beware comparing either serialized or deserialized values.
The test suite is built in CLJC, and is run under both Clojure and Clojurescript.
The Clojurescript unit tests require NodeJS to run. Otherwise only Leiningen is required.
lein test
runs the tests under Clojure.lein cljs-test
runs the tests under Clojurescript and NodeJS.lein test-all
runs both Clojure and Clojurescript test suite.lein cljs-auto-test
automatically compiles and runs the Clojurescript tests on every change.(run-tests)
in the bencode-cljc.core-test
namespace.Copyright © 2017 James Leonis
Distributed under the EPLv2 license. See LICENSE file.
Can you improve this documentation?Edit on GitLab
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close