aboutsummaryrefslogtreecommitdiff
path: root/src/encoding
diff options
context:
space:
mode:
authorCarlos C <uldericofilho@gmail.com>2016-08-10 16:24:11 +0200
committerBrad Fitzpatrick <bradfitz@golang.org>2016-08-16 00:22:47 +0000
commit7a974a4c602252ca7db5b35c1236b45ead4c1d54 (patch)
tree5541760486b70fe5519ff367ab1f4d3dba394b6b /src/encoding
parent3357a02b74fea42c3348de83af81900c6623f584 (diff)
downloadgo-7a974a4c602252ca7db5b35c1236b45ead4c1d54.tar.xz
encoding/json: add example for RawMessage marshalling
Fixes #16648 Change-Id: I3ab21ab33ca3f41219de9518ac6a39f49131e5e5 Reviewed-on: https://go-review.googlesource.com/26692 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/json/example_test.go26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/encoding/json/example_test.go b/src/encoding/json/example_test.go
index 555eff93c0..e4dffd942d 100644
--- a/src/encoding/json/example_test.go
+++ b/src/encoding/json/example_test.go
@@ -174,7 +174,7 @@ func ExampleDecoder_Decode_stream() {
}
// This example uses RawMessage to delay parsing part of a JSON message.
-func ExampleRawMessage() {
+func ExampleRawMessage_unmarshal() {
type Color struct {
Space string
Point json.RawMessage // delay parsing until we know the color space
@@ -219,6 +219,30 @@ func ExampleRawMessage() {
// RGB &{98 218 255}
}
+// This example uses RawMessage to use a precomputed JSON during marshal.
+func ExampleRawMessage_marshal() {
+ h := json.RawMessage(`{"precomputed": true}`)
+
+ c := struct {
+ Header *json.RawMessage `json:"header"`
+ Body string `json:"body"`
+ }{Header: &h, Body: "Hello Gophers!"}
+
+ b, err := json.MarshalIndent(&c, "", "\t")
+ if err != nil {
+ fmt.Println("error:", err)
+ }
+ os.Stdout.Write(b)
+
+ // Output:
+ // {
+ // "header": {
+ // "precomputed": true
+ // },
+ // "body": "Hello Gophers!"
+ // }
+}
+
func ExampleIndent() {
type Road struct {
Name string