Skip to content

Commit

Permalink
Encode unknown terms as null
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Aug 28, 2024
1 parent c5b7c06 commit 80e9092
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/jsxrecord.erl
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ encode_json(Term) ->
skip_values => [],
proplists => true,
encode_list => fun encode_list/2,
encode_tuple => fun encode_tuple/2
% error_handler => fun jsx_error/3
encode_tuple => fun encode_tuple/2,
encode_pid => fun encode_unknown/2,
encode_port => fun encode_unknown/2,
encode_reference => fun encode_unknown/2,
encode_term => fun encode_unknown/2
},
euneus:encode_to_iodata(Term, Options).

Expand Down Expand Up @@ -138,18 +141,15 @@ encode_tuple(T, _Opts) ->
<<"_list">> => tuple_to_list(T)
}).

% jsx_error(throw, {{token, Token}, Rest, Opts, Input, Pos, Buffer}, _Stacktrace) ->
% ?LOG_ERROR(#{
% in => jsxrecord,
% text => <<"Error mapping value to JSON">>,
% result => error,
% reason => json_token,
% token => Token
% }),
% Replacement = null,
% euneus_decoder:resume(Token, Replacement, Rest, Opts, Input, Pos, Buffer);
% jsx_error(Class, Reason, Stacktrace) ->
% euneus_decoder:handle_error(Class, Reason, Stacktrace).
encode_unknown(Term, _State) ->
?LOG_WARNING(#{
in => jsxrecord,
text => <<"Error mapping value to JSON - replaced with 'null'">>,
result => error,
reason => json_token,
token => Term
}),
<<"null">>.

reconstitute_records(Acc, OldAcc) ->
MaybeRecord = case maps:from_list(Acc) of
Expand Down
8 changes: 7 additions & 1 deletion test/jsxrecord_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ all() ->
times,
proplist,
record_proplist,
mixed_list
mixed_list,
unknown_term
].

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -132,6 +133,11 @@ mixed_list(_Config) ->
[ match = re:run(JSON, RE, [{capture, none}]) || RE <- E ],
ok.

unknown_term(_Config) ->
<<"null">> = encode(self()),
<<"null">> = encode(make_ref()),
<<"[null,1,null]">> = encode([ make_ref(), 1, self() ]).

%%--------------------------------------------------------------------
%% SUPPORT FUNCTIONS
%%--------------------------------------------------------------------
Expand Down

0 comments on commit 80e9092

Please sign in to comment.