summaryrefslogtreecommitdiff
path: root/optional/example_json_test.go
blob: d4de4a67ae54fae2b9958faab16e16aa7ce9623a (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
36
37
38
package optional_test

import (
	"encoding/json"
	"fmt"
	"log"

	"xengineering.eu/optional-go/optional"
)

type Message struct {
	Present optional.Optional[string]
	Null    optional.Optional[bool]
	Missing optional.Optional[int]
}

func Example_json() {
	text := `{
		"present": "value",
		"null": null
	}`

	var msg Message

	err := json.Unmarshal([]byte(text), &msg)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf(
		"%t %t %t",
		msg.Present.Exists,
		msg.Null.Exists,
		msg.Missing.Exists,
	)

	// Output: true false false
}