A 100% Clojure-native implementation of the Erlang/OTP External Term Format (ETF)
As expressed in the Erlang docs, the external term format is mainly used in the distribution mechanism of Erlang. It is almost never used in isolation.
That being said, there are numerous situations that fall into the "almost never" category, and as such, this library is provided as a convenience to the intrepid adventurers of Almost Never, so they they may avoid the pain of having to extract the necessary code from the bowels of yet another Erlang/OTP interop library that failed to provide a public API for this.
A quick demonstration of how this library may be used to encode and deocde Erlang terms in Clojure can be seen with the following:
1> Data = [{memory, erlang:memory()},
{process_info, process_info(self())},
{loaded_apps, application:loaded_applications()},
{kernel_env, application:get_all_env(kernel)}].
2> ok = file:write_file("/tmp/erlang_data.bin", term_to_binary(Data)).
Then, in Clojure:
(require '[clojure.java.io :as io]
'[clojang.etf :as etf]
'[clojure.pprint :as pp])
(def data (with-open [is (io/input-stream "/tmp/erlang_data.bin")]
(let [bytes (byte-array (.available is))]
(.read is bytes)
(etf/decode bytes))))
(pp/pprint data)
[{:elements
[{:name "memory"}
[{:elements [{:name "total"} 45983798]}
{:elements [{:name "processes"} 25589256]}
{:elements [{:name "processes_used"} 25587944]}
{:elements [{:name "system"} 20394542]}
{:elements [{:name "atom"} 313413]}
{:elements [{:name "atom_used"} 313413]}
{:elements [{:name "binary"} 300072]}
{:elements [{:name "code"} 6079917]}
{:elements [{:name "ets"} 428160]}]]}
{:elements
[{:name "process_info"}
...]}]
Note that, without pretty printing, you will see data such as this:
[#clojang.etf.types.ErlangTuple{:elements [#clojang.etf.types.ErlangAtom{:name "memory"}
[#clojang.etf.types.ErlangTuple{:elements [#clojang.etf.types.ErlangAtom{:name "total"} 45983798]}
#clojang.etf.types.ErlangTuple{:elements [#clojang.etf.types.ErlangAtom{:name "processes"} 25589256]} ... ]]}]
Copyright © 2025 The Clojang Project
Apache License 2.0