Skip to content

Commit 3002c94

Browse files
josevalimwhatyouhideadriansalamon
authored
Apply suggestions from code review
Co-authored-by: Andrea Leopardi <[email protected]> Co-authored-by: Adrian Salamon <[email protected]>
1 parent 9d5198f commit 3002c94

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lib/elixir/lib/json.ex

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@ defprotocol JSON.Encoder do
99
defstruct ...
1010
1111
It is also possible to encode all fields or skip some fields via the
12-
`:except` option, although this should be used carefully to avoid
13-
accidentally leaking private information when new fields are added:
12+
`:except` option:
1413
1514
@derive JSON.Encoder
1615
defstruct ...
16+
17+
> #### Leaking Private Information {: .error}
18+
>
19+
> The `:except` approach should be used carefully to avoid
20+
> accidentally leaking private information when new fields are added.
1721
1822
Finally, if you don't own the struct you want to encode to JSON,
19-
you may use Protocol.derive/3 placed outside of any module:
23+
you may use `Protocol.derive/3` placed outside of any module:
2024
2125
Protocol.derive(JSON.Encoder, NameOfTheStruct, only: [...])
2226
Protocol.derive(JSON.Encoder, NameOfTheStruct)
27+
2328
"""
2429

2530
@undefined_impl_description """
@@ -99,7 +104,7 @@ defprotocol JSON.Encoder do
99104
end
100105

101106
@doc """
102-
A function invoked to encode the given term.
107+
A function invoked to encode the given term to `t:iodata/0`.
103108
"""
104109
def encode(term, encoder)
105110
end
@@ -210,7 +215,7 @@ defmodule JSON do
210215
* `{:invalid_byte, position, byte}` if `binary` contains unexpected byte or invalid UTF-8 byte
211216
* `{:unexpected_sequence, position, bytes}` if `binary` contains invalid UTF-8 escape
212217
"""
213-
@spec decode(binary()) :: {:ok, term()} | decode_error()
218+
@spec decode(binary()) :: {:ok, term()} | {:error, decode_error()}
214219
def decode(binary) when is_binary(binary) do
215220
with {decoded, :ok, rest} <- decode(binary, :ok, []) do
216221
if rest == "" do
@@ -275,13 +280,14 @@ defmodule JSON do
275280
@doc ~S"""
276281
Decodes the given JSON but raises an exception in case of errors.
277282
278-
Returns the decoded content. See `decode!/1` for possible errors.
283+
Returns the decoded content. See `decode/1` for possible errors.
279284
280285
## Examples
281286
282287
iex> JSON.decode!("[null,123,\"string\",{\"key\":\"value\"}]")
283288
[nil, 123, "string", %{"key" => "value"}]
284289
"""
290+
@spec decode!(binary()) :: term()
285291
def decode!(binary) when is_binary(binary) do
286292
case decode(binary) do
287293
{:ok, decoded} ->

0 commit comments

Comments
 (0)