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

Fix doc example for json:encode/2 #9471

Closed

Conversation

williamthome
Copy link
Contributor

This PR fixes/improves the confusing json:encode/2 example.

Copy link
Contributor

github-actions bot commented Feb 22, 2025

CT Test Results

    2 files     97 suites   1h 7m 47s ⏱️
2 190 tests 2 143 ✅ 47 💤 0 ❌
2 556 runs  2 507 ✅ 49 💤 0 ❌

Results for commit fd44094.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@michalmuskala
Copy link
Contributor

Why is it confusing? The shell allows defining functions - if you paste this example into erl, it will work:

$ erl
Erlang/OTP 27 [erts-15.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit] [dtrace]

Eshell V15.2.2 (press Ctrl+G to abort, type help(). for help)
1> encoder([{_, _} | _] = Value, Encode) -> json:encode_key_value_list(Value, Encode);
   encoder(Other, Encode) -> json:encode_value(Other, Encode).
ok
2> custom_encode(Value) -> json:encode(Value, fun(Value, Encode) -> encoder(Value, Encode) end).
ok
3> iolist_to_binary(custom_encode([{a, []}, {b, 1}])).
<<"{\"a\":[],\"b\":1}">>

@williamthome
Copy link
Contributor Author

if you paste this example into erl, it will work:

Hmm, now I see. I was not aware of this capability. I'll close this PR. Thank you!

@dgud
Copy link
Contributor

dgud commented Feb 22, 2025

Hmm, now I see. I was not aware of this capability. I'll close this PR. Thank you!

It is rather new, came with the new shell, 27?

@garazdawi
Copy link
Contributor

It is rather new, came with the new shell, 27?

26!

@williamthome williamthome deleted the json-fix-encode-example branch February 24, 2025 10:10
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 this pull request may close these issues.

4 participants