summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-12-01 20:41:08 +0100
committerxengineering <me@xengineering.eu>2024-12-01 20:41:08 +0100
commit9552e525fd30cd8a7330430d913c4721f8059e38 (patch)
tree6d6c83c9980d840411122a81624cd2b9c7447caf
parent4c500acc06bd01b52ceacd3dc458f4eb7070e381 (diff)
downloadoptional-go-9552e525fd30cd8a7330430d913c4721f8059e38.tar
optional-go-9552e525fd30cd8a7330430d913c4721f8059e38.tar.zst
optional-go-9552e525fd30cd8a7330430d913c4721f8059e38.zip
Add full-file example for JSON parsingHEADv1.0.2main
-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
+}