Reputation: 13
mochijson2:decode(<<"{\"strKey\":\"中国\", \"intKey\":10, \"arrayKey\":[1, 2, 3]}">>).
** exception throw: invalid_utf8
in function mochijson2:tokenize_string_fast/2 (src/mochijson2.erl, line 424)
in call from mochijson2:tokenize_string/2 (src/mochijson2.erl, line 391)
in call from mochijson2:decode1/2 (src/mochijson2.erl, line 326)
in call from mochijson2:decode_object/3 (src/mochijson2.erl, line 354)
in call from mochijson2:json_decode/2 (src/mochijson2.erl, line 321)
Upvotes: 0
Views: 161
Reputation: 9289
It is not mochijson2 fault.
When you input utf8 binary through the Erlang console, you have to explicitly tell the shell to interpret it is as utf8 by adding /utf8
at the end of the binary. If you don't do that, it tries to interpret it in some strange way.
1> <<"中国">>.
<<"-ý">> % wrong representation
2> <<"中国"/utf8>>.
<<228,184,173,229,155,189>> % ok representation
The problem arises only when using Erlang shell. If you put the same binary in a file without /utf8
, it would work without any problems.
So, in Erlang shell, try this:
1> mochijson2:decode(<<"{\"strKey\":\"中国\", \"intKey\":10, \"arrayKey\":[1, 2, 3]}"/utf8>>).
{struct,[{<<"strKey">>,<<228,184,173,229,155,189>>},
{<<"intKey">>,10},
{<<"arrayKey">>,[1,2,3]}]}
Upvotes: 1