mirror of
https://github.com/foomo/contentserver.git
synced 2025-10-16 12:25:44 +00:00
84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
// 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 jjson_test
|
|
|
|
import (
|
|
"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!
|
|
}
|