-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Add JSON encoding #14021
Add JSON encoding #14021
Conversation
lib/elixir/test/elixir/json_test.exs
Outdated
assert ["{\"a\":", _, ",\"b\":", _, ",\"d\":", _, 125] = | ||
json = JSON.encode_to_iodata(%WithOnly{a: :a, b: "b", c: make_ref(), d: [?d]}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalmuskala you can see I chose to encode the longest possible part of the field as a binary, which includes the preceding field (either {
or ,
), the quotes, and the trailing :
.
Co-authored-by: Eksperimental <[email protected]>
Co-authored-by: Wojtek Mach <[email protected]>
:elixir_json.decode(binary, acc, Map.new(decoders)) | ||
catch | ||
:error, :unexpected_end -> | ||
{:error, {:unexpected_end, byte_size(binary)}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered a JSON.Error or JSON.DecodeError exception with these, matching Jason.DecodeError? In Req we gracefully handle json errors like this:
iex> Req.get(plug: & &1 |> Plug.Conn.put_resp_content_type("application/json") |> Plug.Conn.send_resp(200, "{"))
{:error, %Jason.DecodeError{position: 1, token: nil, data: "{"}}
(which I'm still somewhat skeptical of cause it's not like users can do much with these errors anyway but this was a somewhat common request to have this graceful handling)
so it'd be nice to have an exception struct I could return instead. Not a big deal though, I can invent one for Req.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can raise a specific error on JSON.decode!
, and I will make it so, but we don't use the {:error, Exception.t()}
struct style anywhere in Elixir, so I am not sure we should do it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, thanks!
lib/elixir/lib/json.ex
Outdated
|
||
@moduledoc since: "1.18.0" | ||
|
||
@type decode_error :: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call this decode_error_reason
? We could also document the reasons here maybe.
lib/elixir/lib/json.ex
Outdated
"[123,\"string\",{\"key\":\"value\"}]" | ||
""" | ||
def encode!(term, encoder \\ &encode_value/2) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spec?
Co-authored-by: Andrea Leopardi <[email protected]> Co-authored-by: Adrian Salamon <[email protected]>
lib/elixir/lib/json.ex
Outdated
@doc """ | ||
This is the default function used to recursively encode each value. | ||
""" | ||
def encode_value(value, encoder) when is_atom(value) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is the same as the protocol, inlined for performance. But I am not a fan of its name. Suggestions? What about encode_callback
? or default_encode
? or default_encode_callback
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given it isn't technically an @callback
, even if it is a generic term _callback
might be confusing since its specific meaning in OTP.
default_encode
captures the intent well I think.
💚 💙 💜 💛 ❤️ |
"[123,\"string\",{\"key\":\"value\"}]" | ||
""" | ||
@spec encode!(a, (a -> iodata())) :: binary() when a: var |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This spec seems wrong, the callback takes two arguments, not one. Same on line 356
Note to code reviewers: no need to review the
elixir_json.erl
module, that was ported from Erlang for backwards compatibility reasons. It will be removed in future Elixir versions.encode_value
cc @michalmuskala