summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--optional/example_json_test.go38
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
+}