-
Notifications
You must be signed in to change notification settings - Fork 4
Encoding
Eric Pailleau edited this page Jan 6, 2018
·
25 revisions
jason:encode/1
jason:encode(Term) -> string() | no_return()jason:encode/2
jason:encode(Term, Options) -> list() | no_return() | {ok, list()} | {error, Reason}
Reason = {'invalid_term', Term, Depth} | {unable_to_encode, Term, Depth}jason:encode_file/2
jason:encode_file(Term, TargetFile) -> ok | {error, Reason}
Reason = posix() | badarg | terminated | system_limitjason:encode_file/3
jason:encode_file(Term, TargetFile, Options) -> ok | {error, Reason}
Reason = posix() | badarg | terminated | system_limitArity 2 functions accept a property list of {key, Value} tuples.
| Key | Value Type | Value | Comment |
|---|---|---|---|
mode |
atom() |
struct, proplist, map, record
|
struct default. |
indent |
atom() |
true, false
|
false (compact) default. |
records |
{atom(), [atom(), ...]} |
[{RecName, RecFields}, ...] |
List of Record definitions |
records |
atom() or [atom(), ...]
|
Module name(s) where record(s) declared | Extract record info from module(s) (slower) |
Any Erlang term that can be translated to JSON, excluding tuple with more or less than two elements, if no records declared.
Example :
1> jason:encode([{a,b}]).
"{\"a\": \"b\"}"
2> catch jason:encode([{a,b,c}]).
{unable_to_encode,a,1}
3> A = jason:encode({a,"b", "c"}, [{records, [{a, [b,c]}]}]).
"{\"b\": \"b\",\"c\": \"c\"}"
4> jason:decode(A, [{mode, record}, {records, [{a, [b,c]}]}]).
{a,"b","c"}
% Success
1> jason:encode([1,2,3,a]).
"[1,2,3,\"a\"]"
2> jason:encode([1,2,3,a], [{return, tuple}]).
{ok, "[1,2,3,\"a\"]"}% Error
1> catch jason:encode([1,2,3,{a}]).
{invalid_term,{a},1}
2> jason:encode([1,2,3,{a}], [{return, tuple}]).
{error, {invalid_term,{a},1}}