-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
39 lines (38 loc) · 913 Bytes
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Package omit provides a type which can be used to represent a value which may or may not be set.
// This is useful for omitting the value in JSON if it can be null or omitted since pointers can't represent this.
//
// The zero value of Omit is not set.
//
// Example:
//
// type user struct {
// ID Omit[int] `json:"id,omitzero"`
// Name Omit[*string] `json:"name,omitzero"`
// }
//
// json.Marshal(user{
// ID: New(1),
// Name: NewPtr("john"),
// })
//
// // Output: {"id":1,"name":"john"}
//
// json.Marshal(user{
// ID: NewZero[int](),
// Name: NewNilPtr[string](),
// })
//
// // Output: {"name":null}
//
// var u user
// json.Unmarshal([]byte(`{"id":1,"name":"john"}`), &u)
// fmt.Println(u.ID.Value, u.Name.Value)
//
// // Output: 1 john
//
// json.Unmarshal([]byte(`{"name":null}`), &u)
// fmt.Println(u.ID, u.Name)
//
// // Output: <omitted> <nil>
//
package omit