Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for other Erlang datatypes (pid, port, func, etc) #64

Closed
menih opened this issue Aug 27, 2021 · 6 comments · Fixed by #65
Closed

Support for other Erlang datatypes (pid, port, func, etc) #64

menih opened this issue Aug 27, 2021 · 6 comments · Fixed by #65

Comments

@menih
Copy link

menih commented Aug 27, 2021

Would be great if we can convert them all to a string. I'm trying to encode process_info(self()). It crashes on pids.

@sile
Copy link
Owner

sile commented Aug 28, 2021

You can use map_unknown_value option for that purpose as follows:

% Without `map_unknown_value`
>  jsone:encode([1,self(),foo]).
** exception error: bad argument

% With `map_unknown_value`
> jsone:encode([1,self(),foo], [{map_unknown_value, fun (X) -> {ok, list_to_binary(io_lib:format("~p", [X]))} end}]).
<<"[1,\"<0.209.0>\",\"foo\"]">>

@menih
Copy link
Author

menih commented Aug 28, 2021 via email

@sile
Copy link
Owner

sile commented Aug 28, 2021

It would be nice if this is the default behavior.

Maybe.
However, I'd like to keep jsone implementation simple as possible (it's already a bit complex though..), and I don't know how many users desire this behavior to be the default.
So let me keep the implementation as-is for a while. I'll reconsider that if I receive more similar requests in the future.

@menih
Copy link
Author

menih commented Aug 28, 2021 via email

@sile
Copy link
Owner

sile commented Aug 28, 2021

Thanks for your reply. The feedback is very useful.

On the second thought, I think that it might be better to define a function, like below, and set it to the default value of map_unknown_value option.

default_map_unknown_value(X) ->
    {ok, list_to_binary(io_lib:format("~p", [X]))}.

This approach doesn't have to modify the core part of the jsone encoding logic, and users can easily opt out of the behavior (or use other encoding formats they prefer).
I'll consider that more deeply and create a prototype PR when I have time.

@menih
Copy link
Author

menih commented Aug 28, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants