From 9552e525fd30cd8a7330430d913c4721f8059e38 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 1 Dec 2024 20:41:08 +0100 Subject: Add full-file example for JSON parsing --- optional/example_json_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 optional/example_json_test.go diff --git a/optional/example_json_test.go b/optional/example_json_test.go new file mode 100644 index 0000000..d4de4a6 --- /dev/null +++ b/optional/example_json_test.go @@ -0,0 +1,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 +} -- cgit v1.2.3-70-g09d2