我正在尝试在 Elixir 程序和 C 程序之间移动一些数据。在 Elixir 中,我有一个我想要移动的东西的结构。我使用 :erlang.term_to_binary
将其转换为二进制文件,然后将其混搭为 C。
但是,当我解码它时,它只返回NULL
??并且没有错误消息或任何内容。一堆其他的 elixir/erlang 术语工作得很好;当我尝试发送 map 或结构(这是一种 map )时,它特别失败。
我在 Elixir 中做这样的事情:
msg = %Message{ title: "hello", body: "world" }
binmsg = :erlang.term_to_binary(msg)
send(state.port, {self(), {:command, binmsg}})
.. 并且在 C 中(不包括消息的接收,我已经确认它已经以正确的长度到达,甚至确认字节在 C 和 elixir 中是相同的):
uint8_t *buf = read_cmd();
ETERM *map = erl_decode(buf);
erl_print_term(map, stdout);
最佳答案
好像from the source code of erl_decode它不支持反序列化 map 。 map 是 defined in Erlang's binary protocol从 Erlang/OTP 17 开始,但是 erl_marshal.c
中没有提到 MAP_EXT。
这可能是因为 erl_marshal
是 hella legacy,自 Erlang/OTP 22 以来已被弃用。请改用 ei_decode_*
函数系列!甚至还有一个 ei_decode_map_header
.这应该有效。
关于serialization - 为什么在反序列化 Elixir 映射时 `erl_decode` 返回 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60677657/