alavrik / erlson

Erlang Simple Object Notation - dynamic name-value dictionary data type and syntax for Erlang
MIT License
81 stars 17 forks source link

Bug of function erlson:from_json and erlson:from_json_term #14

Closed gemSender closed 10 years ago

gemSender commented 10 years ago

erlson:from_json( <<"{\"buildingType\":\"goldCollector\",\"buildingId\":1,\"result\":null}">>). the return value is [{buildingType,<<"goldCollector">>},{result,undefined}, {<<"buildingId">>,1}]. It should be [{buildingType,<<"goldCollector">>},{result,undefined}, {buildingId,1}].

gemSender commented 10 years ago

why did you use binary_to_existing_atom rather than binary_to_atom

alavrik commented 10 years ago

This is a documented behavior. Here's a relevant quote from README.md:

Erlson uses Erlang built-in binary_to_existing_atom/1 function for converting JSON field names into Erlang atoms. This is to prevent the VM from running out of atom space on malicious JSON input.

Generally, everything should just work out of the box in compiled projects. However, if you are using Erlson from Erlang shell, not all of your modules and correspondent atoms may be loaded. In this case, JSON field names for unknown atoms will be represented as binaries after parsing. To fix that, you can load all the necessary modules. This can be done, for example, by running this command:

Modules = [application:get_key(A, modules) || A <- application:loaded_applications()],
[ code:load(M) || lists:append(Modules) ].
gemSender commented 10 years ago

Got it,thank you!