-->

undefined function maps:to_json/1

2020-08-13 05:49发布

问题:

This is one of the things I learned from Programming Erlang, 2nd Ed, but doesn't seem to work on shell

$ erl
Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V6.2  (abort with ^G)
1> Dic = #{b => 1, a => 2}.
#{a => 2,b => 1}
2> maps:to_json(Dic).
** exception error: undefined function maps:to_json/1
3> 

What's wrong here?

回答1:

Programming Erlang, 2nd Ed was written before maps were introduced in release 17 (as experimental feature) and it seems that author of book, Joe Armstrong was too optimistic about them. Still some of proposed functionality and syntax is not yet implemented (or never will be).

Luckily, there are JSON parsers for erlang which support maps, check:

  • jiffy
  • jsx


回答2:

Another library (easiest to use imo) is jsone:

jsone github



标签: erlang