2011-12-19 11:16:55 +11:00
|
|
|
// 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 (
|
2014-05-08 16:52:36 +10:00
|
|
|
"bytes"
|
2011-12-19 11:16:55 +11:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2012-02-18 11:48:33 +11:00
|
|
|
"io"
|
|
|
|
|
"log"
|
2011-12-19 11:16:55 +11:00
|
|
|
"os"
|
2012-02-18 11:48:33 +11:00
|
|
|
"strings"
|
2011-12-19 11:16:55 +11:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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)
|
2012-02-16 11:50:28 +11:00
|
|
|
// Output:
|
|
|
|
|
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
2011-12-19 11:16:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
2012-02-16 11:50:28 +11:00
|
|
|
// Output:
|
|
|
|
|
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
2011-12-19 11:16:55 +11:00
|
|
|
}
|
2012-02-18 11:48:33 +11:00
|
|
|
|
|
|
|
|
// 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!
|
|
|
|
|
}
|
2013-10-03 08:52:18 +10:00
|
|
|
|
|
|
|
|
// 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}
|
|
|
|
|
}
|
2014-05-08 16:52:36 +10:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
// = }
|
|
|
|
|
// =]
|
|
|
|
|
}
|