Skip to content

Commit 811767f

Browse files
authored
Update JSON.Encoder moduledoc (#14784)
1 parent 20c7d07 commit 811767f

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lib/elixir/lib/json.ex

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,29 @@ defprotocol JSON.Encoder do
88
If you have a struct, you can derive the implementation of this protocol
99
by specifying which fields should be encoded to JSON:
1010
11-
@derive {JSON.Encoder, only: [....]}
11+
@derive {JSON.Encoder, only: [...]}
1212
defstruct ...
1313
14-
It is also possible to encode all fields or skip some fields via the
15-
`:except` option:
14+
Additionally, you can exclude specific fields using the `:except` option or
15+
encode all fields by omitting both options entirely, but these should be used
16+
with caution:
17+
18+
@derive {JSON.Encoder, except: [...]}
19+
defstruct ...
1620
1721
@derive JSON.Encoder
1822
defstruct ...
1923
2024
> #### Leaking Private Information {: .error}
2125
>
22-
> The `:except` approach should be used carefully to avoid
23-
> accidentally leaking private information when new fields are added.
26+
> Prefer using `:only` to avoid accidentally leaking private information when
27+
> new fields are added. Other approaches should be used with auction.
2428
25-
Finally, if you don't own the struct you want to encode to JSON,
26-
you may use `Protocol.derive/3` placed outside of any module:
29+
You can also use `Protocol.derive/3` if you don't own the struct that you want
30+
to encode to JSON:
2731
2832
Protocol.derive(JSON.Encoder, NameOfTheStruct, only: [...])
33+
Protocol.derive(JSON.Encoder, NameOfTheStruct, except: [...])
2934
Protocol.derive(JSON.Encoder, NameOfTheStruct)
3035
3136
"""

0 commit comments

Comments
 (0)