Browse Source

Merge pull request #52 from sile/add-attempt-atom-to-readme

Add an `attempt_atom` option example to README.md
Takeru Ohta 4 years ago
parent
commit
56416ee2b0
1 changed files with 10 additions and 0 deletions
  1. 10 0
      README.md

+ 10 - 0
README.md

@@ -170,6 +170,16 @@ ok
 
 > jsone:encode(1.23, [{float_format, [{decimals, 4}, compact]}]). % compact decimal notation
 <<"1.23">>
+
+%% If you want to safely cast object keys to atoms, the `attempt_atom' option will help.
+> jsone:decode(<<"{\"hello\": \"world\"}">>, [{keys, attempt_atom}]).
+#{<<"hello">> => <<"world">>}  % There is no atom named "hello", so the key is decoded as binary.
+
+> hello.  % Create "hello" atom.
+hello
+
+> jsone:decode(<<"{\"hello\": \"world\"}">>, [{keys, attempt_atom}]).
+#{hello => <<"world">>} % Now, the key is decoded as atom.
 ```