blob: 23aa780212a0d20ee32de93f6626a46c226ac873 (
plain)
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
|
package optional
import (
"encoding/json"
)
func (o *Optional[T]) UnmarshalJSON(data []byte) error {
var Buffer *T
err := json.Unmarshal(data, &Buffer)
if err != nil {
return err
}
if Buffer == nil {
var empty T
o.Value = empty
o.Exists = false
return nil
}
o.Value = *Buffer
o.Exists = true
return nil
}
func (o Optional[T]) MarshalJSON() ([]byte, error) {
var Buffer *T
if o.Exists {
Buffer = &o.Value
}
return json.Marshal(Buffer)
}
|