-
Notifications
You must be signed in to change notification settings - Fork 4
Decoding
Eric Pailleau edited this page Aug 12, 2017
·
28 revisions
jason:decode/1jason:decode/2jason:decode_file/1jason:decode_file/2
Arity 2 functions accept a property list of {key, Value} tuples.
| key | value | Comment |
|---|---|---|
mode |
struct, proplist, map, record
|
struct default. |
By default, returns Erlang term on success, or throw {Line, Reason} exception on error.
Using option {return, tuple} let return {ok, Term} or {error, {Line, Reason}}.
1> jason:decode('{"ab": "cd"}').
[{<<"ab">>,<<"cd">>}]
2> jason:decode('{"ab": "cd"}', [{return, tuple}]).
{ok,[{<<"ab">>,<<"cd">>}]}1> jason:decode('{"ab": "cd"').
** exception throw: {1,"syntax error before end"}
in function jason:decode/2 (src/jason.erl, line 211)
2> jason:decode('{"ab": "cd"', [{return, tuple}]).
{error,{1,"syntax error before end"}}jason:decode/x functions can accept binary, string and even atom data to decode.
Tip : it is worth noting that using atom in Erlang shell is easier than using string or binary for character escaping . For your mental health, use atoms in the shell for short JSON statements !
1> jason:decode('["a","b\\""]'). % Atom (but limited to 255 characters !)
{ok,[<<"a">>,<<"b\"">>]}
2> jason:decode(<<"[\"a\",\"b\\\"\"]">>). % Binary
{ok,[<<"a">>,<<"b\"">>]}
3> jason:decode("[\"a\",\"b\\\"\"]"). % String
{ok,[<<"a">>,<<"b\"">>]}