sile / jsone

Erlang JSON library
MIT License
291 stars 72 forks source link

json decode exception : if value contains white space #6

Closed aposto closed 9 years ago

aposto commented 9 years ago
jsone:decode(<<"{\"_comment_Car_Card\" : \"CONTENTSDB\"}">>). => ok.

jsone:decode(<<"{\"_comment_Car_Card\" : \"\tCONTENTSDB\"}">>). => error. (value contains \t )

** exception error: no function clause matching jsone_decode:string(<<"\tCONTENTSDB\"}">>,<<"\tCONTENTSDB\"}">>,0,
                                                                    [{object_next,<<"_comment_Car_Card">>,[]}],
                                                                    <<>>,
                                                                    {decode_opt_v1,map}) (src/jsone_decode.erl, line 139)
     in function  jsone:decode/2 (src/jsone.erl, line 228)

The expected value is [{<<"_comment_Car_Card">>,<<"\tCONTENTSDB">>}] .

sile commented 9 years ago

The JSON specification (RFC7159) says:

characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F)

So, it is an error of as expected.

But for convenience, I added allow_ctrl_chars decoding option from version 1.2.0. The option allows strings which contain unescaped control characters.

%% Example

% The default value is false
> jsone:decode(<<"{\"_comment_Car_Card\" : \"\tCONTENTSDB\"}">>). 
** exception error: bad argument
     in function  jsone_decode:string/6
        called as jsone_decode:string(<<"\tCONTENTSDB\"}">>,<<"\tCONTENTSDB\"}">>,0,
                                      [{object_next,<<"_comment_Car_Card">>,[]}],
                                      <<>>,
                                      {decode_opt_v2,map,false})
     in call from jsone:decode/2 (/home/ohta/dev/erlang/jsone/_build/default/lib/jsone/src/jsone.erl, line 208)

% Sets `allow_ctrl_chars` to true
> jsone:decode(<<"{\"_comment_Car_Card\" : \"\tCONTENTSDB\"}">>, [{allow_ctrl_chars, true}]).
#{<<"_comment_Car_Card">> => <<"\tCONTENTSDB">>}
aposto commented 9 years ago

Oh. It's a awesome option. Thank you.

sile commented 9 years ago

You're welcome.