summaryrefslogtreecommitdiff
path: root/src/pkg/encoding/json/example_test.go
diff options
context:
space:
mode:
authorTianon Gravi <admwiggin@gmail.com>2015-01-15 11:54:00 -0700
committerTianon Gravi <admwiggin@gmail.com>2015-01-15 11:54:00 -0700
commitf154da9e12608589e8d5f0508f908a0c3e88a1bb (patch)
treef8255d51e10c6f1e0ed69702200b966c9556a431 /src/pkg/encoding/json/example_test.go
parent8d8329ed5dfb9622c82a9fbec6fd99a580f9c9f6 (diff)
downloadgolang-upstream/1.4.tar.gz
Imported Upstream version 1.4upstream/1.4
Diffstat (limited to 'src/pkg/encoding/json/example_test.go')
-rw-r--r--src/pkg/encoding/json/example_test.go161
1 files changed, 0 insertions, 161 deletions
diff --git a/src/pkg/encoding/json/example_test.go b/src/pkg/encoding/json/example_test.go
deleted file mode 100644
index ca4e5ae68..000000000
--- a/src/pkg/encoding/json/example_test.go
+++ /dev/null
@@ -1,161 +0,0 @@
-// Copyright 2011 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package json_test
-
-import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "log"
- "os"
- "strings"
-)
-
-func ExampleMarshal() {
- type ColorGroup struct {
- ID int
- Name string
- Colors []string
- }
- group := ColorGroup{
- ID: 1,
- Name: "Reds",
- Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
- }
- b, err := json.Marshal(group)
- if err != nil {
- fmt.Println("error:", err)
- }
- os.Stdout.Write(b)
- // Output:
- // {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
-}
-
-func ExampleUnmarshal() {
- var jsonBlob = []byte(`[
- {"Name": "Platypus", "Order": "Monotremata"},
- {"Name": "Quoll", "Order": "Dasyuromorphia"}
- ]`)
- type Animal struct {
- Name string
- Order string
- }
- var animals []Animal
- err := json.Unmarshal(jsonBlob, &animals)
- if err != nil {
- fmt.Println("error:", err)
- }
- fmt.Printf("%+v", animals)
- // Output:
- // [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
-}
-
-// This example uses a Decoder to decode a stream of distinct JSON values.
-func ExampleDecoder() {
- const jsonStream = `
- {"Name": "Ed", "Text": "Knock knock."}
- {"Name": "Sam", "Text": "Who's there?"}
- {"Name": "Ed", "Text": "Go fmt."}
- {"Name": "Sam", "Text": "Go fmt who?"}
- {"Name": "Ed", "Text": "Go fmt yourself!"}
- `
- type Message struct {
- Name, Text string
- }
- dec := json.NewDecoder(strings.NewReader(jsonStream))
- for {
- var m Message
- if err := dec.Decode(&m); err == io.EOF {
- break
- } else if err != nil {
- log.Fatal(err)
- }
- fmt.Printf("%s: %s\n", m.Name, m.Text)
- }
- // Output:
- // Ed: Knock knock.
- // Sam: Who's there?
- // Ed: Go fmt.
- // Sam: Go fmt who?
- // Ed: Go fmt yourself!
-}
-
-// This example uses RawMessage to delay parsing part of a JSON message.
-func ExampleRawMessage() {
- type Color struct {
- Space string
- Point json.RawMessage // delay parsing until we know the color space
- }
- type RGB struct {
- R uint8
- G uint8
- B uint8
- }
- type YCbCr struct {
- Y uint8
- Cb int8
- Cr int8
- }
-
- var j = []byte(`[
- {"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10}},
- {"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255}}
- ]`)
- var colors []Color
- err := json.Unmarshal(j, &colors)
- if err != nil {
- log.Fatalln("error:", err)
- }
-
- for _, c := range colors {
- var dst interface{}
- switch c.Space {
- case "RGB":
- dst = new(RGB)
- case "YCbCr":
- dst = new(YCbCr)
- }
- err := json.Unmarshal(c.Point, dst)
- if err != nil {
- log.Fatalln("error:", err)
- }
- fmt.Println(c.Space, dst)
- }
- // Output:
- // YCbCr &{255 0 -10}
- // RGB &{98 218 255}
-}
-
-func ExampleIndent() {
- type Road struct {
- Name string
- Number int
- }
- roads := []Road{
- {"Diamond Fork", 29},
- {"Sheep Creek", 51},
- }
-
- b, err := json.Marshal(roads)
- if err != nil {
- log.Fatal(err)
- }
-
- var out bytes.Buffer
- json.Indent(&out, b, "=", "\t")
- out.WriteTo(os.Stdout)
- // Output:
- // [
- // = {
- // = "Name": "Diamond Fork",
- // = "Number": 29
- // = },
- // = {
- // = "Name": "Sheep Creek",
- // = "Number": 51
- // = }
- // =]
-}