sile / jsone

Erlang JSON library
MIT License
291 stars 71 forks source link

encoding fails with proplists with string keys #76

Closed matteoredaelli closed 2 years ago

matteoredaelli commented 2 years ago

It is ok with binary keys

(a@127.0.0.1)42> jsone:encode([{<<"a">>, 1}]).                    
<<"{\"a\":1}">>

It fails with string keys

(a@127.0.0.1)43> jsone:encode([{"a", 1}]).    
** exception error: bad argument
     in function  jsone_encode:object_key/4
        called as jsone_encode:object_key("a",
                                          [{object_value,1,[]}],
                                          <<"{">>,
                                          {encode_opt_v2,false,false,false,
                                              [{scientific,20}],
                                              {iso8601,0},
                                              string,0,0,false,false,
                                              fun jsone:term_to_json_string/1})
     in call from jsone:encode/2 (src/jsone.erl, line 386)
sile commented 2 years ago

Yes, jsone doesn't allow Erlang string keys. You need to convert Erlang strings to binaries to use as JSON object keys beforehand