From 9367a11e0606614aa52b00c62c37bb5ea583dfc4 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Sun, 29 Dec 2024 08:38:30 +0530 Subject: [PATCH 1/2] Update json.go adding example for json.NewDecoder --- examples/json/json.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/json/json.go b/examples/json/json.go index 259478632..5efff0c83 100644 --- a/examples/json/json.go +++ b/examples/json/json.go @@ -121,4 +121,11 @@ func main() { enc := json.NewEncoder(os.Stdout) d := map[string]int{"apple": 5, "lettuce": 7} enc.Encode(d) + + // We can also decode JSON directly from `os.Reader` + dec := json.NewDecoder(strings.NewReader(str)) + res1 = response2{} + dec.Decode(&res1) + fmt.Println(res1) + fmt.Println(res1.Fruits[1]) } From e411023e4c97b2606bc60e1b3d4669ce530dbafd Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Sun, 29 Dec 2024 04:35:54 +0000 Subject: [PATCH 2/2] updated as per Contributings.md instructions --- examples/json/json.go | 3 ++- examples/json/json.hash | 4 ++-- public/json | 24 ++++++++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/examples/json/json.go b/examples/json/json.go index 5efff0c83..aac870e77 100644 --- a/examples/json/json.go +++ b/examples/json/json.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "os" + "strings" ) // We'll use these two structs to demonstrate encoding and @@ -124,7 +125,7 @@ func main() { // We can also decode JSON directly from `os.Reader` dec := json.NewDecoder(strings.NewReader(str)) - res1 = response2{} + res1 := response2{} dec.Decode(&res1) fmt.Println(res1) fmt.Println(res1.Fruits[1]) diff --git a/examples/json/json.hash b/examples/json/json.hash index 60f6c01bd..6958f8dad 100644 --- a/examples/json/json.hash +++ b/examples/json/json.hash @@ -1,2 +1,2 @@ -35295476f817fe575619b6168273a29eddd7f545 -JOQpRGJWAxR +75a5664321dafe10fa623c187b90f079c5319a3c +jR7IEUPQF8T diff --git a/public/json b/public/json index b5d0f5815..55f2280ce 100644 --- a/public/json +++ b/public/json @@ -46,7 +46,7 @@ data types.

- +
package main
@@ -61,6 +61,7 @@ data types.

"encoding/json" "fmt" "os" + "strings" ) @@ -313,11 +314,26 @@ stream JSON encodings directly to os.Writers like os.Stdout or even HTTP response bodies.

- +
    enc := json.NewEncoder(os.Stdout)
     d := map[string]int{"apple": 5, "lettuce": 7}
-    enc.Encode(d)
+    enc.Encode(d)
+ + + + + +

We can also decode JSON directly from os.Reader

+ + + + +
    dec := json.NewDecoder(strings.NewReader(str))
+    res1 := response2{}
+    dec.Decode(&res1)
+    fmt.Println(res1)
+    fmt.Println(res1.Fruits[1])
 }
@@ -379,7 +395,7 @@ for more.