Skip to content

Commit 5342e55

Browse files
committed
Docs
1 parent ad27f73 commit 5342e55

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib/elixir/lib/json.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
defprotocol JSON.Encoder do
22
@moduledoc """
33
A protocol for custom JSON encoding of data structures.
4+
5+
If you have a struct, you can derive the implementation of this protocol
6+
by specifying which fields should be encoded to JSON:
7+
8+
@derive {JSON.Encoder, only: [....]}
9+
defstruct ...
10+
11+
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:
14+
15+
@derive JSON.Encoder
16+
defstruct ...
17+
18+
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:
20+
21+
Protocol.derive(JSON.Encoder, NameOfTheStruct, only: [...])
22+
Protocol.derive(JSON.Encoder, NameOfTheStruct)
423
"""
524

625
@undefined_impl_description """

0 commit comments

Comments
 (0)