basilisp.contrib.bencode¶
Support for bencode encoding and decoding.
- fn (decode data {:as opts :keys [keywordize-keys key-fn]})[source]¶
Decode the first value in the bencoded
data
bytes according toopts
and return a[decoded* rest*]
vector.The decoded* item in the vector is the decoded value of the first item in
data
while rest* is the remaining unencoded values.If
data
cannot be decoded (e.g. is incomplete or an error occurred), it returns a[nil data]
vector.opts
is a map with the following optional supported keys.- keyword
:keywordize-keys
: if the decoded value is a map, keywordize its keys.
- keyword
:key-fn
: a function which will be called for each key-in a map; cannot be specified if
:keywordize-keys
is also specified- keyword
:string-fn
: a function which will be called for each byte string which is not a map key; default is
basilisp.core/identity
- keyword
- fn (decode-all data)[source]¶
- fn (decode-all data opts)
Decode all values in the bencoded
data
bytes and return them as a[values* incomplete*]
vector.The
values*
item is a collection of thedata
decoded values, whileincomplete*
is the rest of thedata
bytes that could not be decoded ornil
.opts
is a map supporting the same keys asdecode
.
- fn (encode d)[source]¶
Encode the object
d
into a byte string usingbencode
encoding.encode
supports encoding the following types:str
, which is first decoded to UTF-8bytes
keywords and symbols, which are first converted to strings (including namespace, separated by ‘/’) and then converted using the rules for
str
sBasilisp lists, vectors, and maps
Mapping type keys must one of: keywords, symbols, or strings.
This function does not support
float
because thebencode
specification does not support non-integer numerics.Set types (including
frozenset
,set
, or Basilisp’s set types) are not supported due to the requirement that lists retain their original element ordering.