"Instead of cursing the darkness, light a candle." ― Benjamin Franklin
A functional Clojure(script) BEncode serialization library.
[com.jamesleonis/bencode-cljc "0.2.2"]
A dependency-free implementation of BEncode. bencode-cljc aims to be a portable implementation 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 runs both Clojure and Clojurescript. Leiningen is deprecated and not guaranteed to work.
To run the Clojure tests, use the Clojure CLI to start the test runner. The CLI tools are all that are required.
clojure -M:test:runner
To run the benchmarks
clojure -M:benchmark-clj
To build and deploy with build.clj
clojure -T:build all
clojure -T:build deploy
The Clojurescript build additionally requires Node.js and Shadow-CLJS. Artifacts will be generated in the build folder
npx shadow-cljs -A:test compile test
To run the benchmarks, use the benchmark tag
npx shadow-cljs -A:benchmark-cljs compile benchmark
To build a release, compile with the lib tag
npx shadow-cljs release lib
build.clj supportdeps.edn and shadow-cljs.edn supportCopyright © 2017-2025 James Leonis
Distributed under the EPLv2 license. See LICENSE file.
Can you improve this documentation?Edit on GitLab
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |