sile / jsone

Erlang JSON library
MIT License
291 stars 72 forks source link

J /= jsone:decode(jsone:encode(J). #83

Closed elsbiet closed 1 year ago

elsbiet commented 1 year ago

hi,

i thought that

J=jsone:decode(jsone:encode(J).

should hold true for any encodeble J but this not true as you can see from

J=[{a,1},{b,2}]. % which is a list of tuples

jsone:encode(J). <<"{\"a\":1,\"b\":2}">>

jsone:decode(jsone:encode(J)).

{<<"a">> => 1,<<"b">> => 2} % which is a map

is there anything i have missed?

e.

sile commented 1 year ago

Because the type mapping between JSON and Erlang is not one-to-one, it is possible that there are cases where J /= jsone:decode(jsone:encode(J)). You can control the types of the decoded Erlang objects by specifying options as follows:

1> J=[{a,1},{b,2}].                    
[{a,1},{b,2}] 

2> jsone:decode(jsone:encode(J), [{object_format, proplist}, {keys, attempt_atom}]).
[{a,1},{b,2}]
elsbiet commented 1 year ago

thanks for your answer.

i was not aware of the fact that json <-> erlang is not one-to-one.

so ithink, i've got to chane my interface data structures in a way that M=jsone:decode(jsone:encode(M)). is fulfilled.