diff options
author | xengineering <me@xengineering.eu> | 2024-12-01 20:41:08 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-12-01 20:41:08 +0100 |
commit | 9552e525fd30cd8a7330430d913c4721f8059e38 (patch) | |
tree | 6d6c83c9980d840411122a81624cd2b9c7447caf | |
parent | 4c500acc06bd01b52ceacd3dc458f4eb7070e381 (diff) | |
download | optional-go-9552e525fd30cd8a7330430d913c4721f8059e38.tar optional-go-9552e525fd30cd8a7330430d913c4721f8059e38.tar.zst optional-go-9552e525fd30cd8a7330430d913c4721f8059e38.zip |
-rw-r--r-- | optional/example_json_test.go | 38 |
1 files changed, 38 insertions, 0 deletions
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 +} |