2017-07-09 11:10:44 +08:00
|
|
|
package jsoniter
|
2017-06-05 20:37:08 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2017-06-05 22:10:01 +08:00
|
|
|
func ExampleMarshal() {
|
2017-06-05 20:37:08 +08:00
|
|
|
type ColorGroup struct {
|
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Colors []string
|
|
|
|
}
|
|
|
|
group := ColorGroup{
|
|
|
|
ID: 1,
|
|
|
|
Name: "Reds",
|
|
|
|
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
|
|
|
}
|
2017-07-09 11:10:44 +08:00
|
|
|
b, err := Marshal(group)
|
2017-06-05 20:37:08 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("error:", err)
|
|
|
|
}
|
|
|
|
os.Stdout.Write(b)
|
|
|
|
// Output:
|
|
|
|
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
|
|
|
}
|
2017-06-08 12:07:03 +08:00
|
|
|
|
2017-06-08 12:08:47 +08:00
|
|
|
func ExampleUnmarshal() {
|
2017-06-08 12:07:03 +08:00
|
|
|
var jsonBlob = []byte(`[
|
|
|
|
{"Name": "Platypus", "Order": "Monotremata"},
|
|
|
|
{"Name": "Quoll", "Order": "Dasyuromorphia"}
|
|
|
|
]`)
|
|
|
|
type Animal struct {
|
2017-06-09 16:25:58 +08:00
|
|
|
Name string
|
|
|
|
Order string
|
2017-06-08 12:07:03 +08:00
|
|
|
}
|
|
|
|
var animals []Animal
|
2017-07-09 11:10:44 +08:00
|
|
|
err := Unmarshal(jsonBlob, &animals)
|
2017-06-08 12:07:03 +08:00
|
|
|
if err != nil {
|
2017-06-09 16:25:58 +08:00
|
|
|
fmt.Println("error:", err)
|
2017-06-08 12:07:03 +08:00
|
|
|
}
|
|
|
|
fmt.Printf("%+v", animals)
|
|
|
|
// Output:
|
|
|
|
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
2017-06-09 16:25:58 +08:00
|
|
|
}
|
2017-06-17 17:14:34 +08:00
|
|
|
|
2017-07-09 11:24:26 +08:00
|
|
|
func ExampleConfigFastest_Marshal() {
|
2017-06-17 17:14:34 +08:00
|
|
|
type ColorGroup struct {
|
|
|
|
ID int
|
|
|
|
Name string
|
|
|
|
Colors []string
|
|
|
|
}
|
|
|
|
group := ColorGroup{
|
|
|
|
ID: 1,
|
|
|
|
Name: "Reds",
|
|
|
|
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
|
|
|
}
|
2017-07-09 11:10:44 +08:00
|
|
|
stream := ConfigFastest.BorrowStream(nil)
|
|
|
|
defer ConfigFastest.ReturnStream(stream)
|
2017-06-17 17:14:34 +08:00
|
|
|
stream.WriteVal(group)
|
|
|
|
if stream.Error != nil {
|
|
|
|
fmt.Println("error:", stream.Error)
|
|
|
|
}
|
|
|
|
os.Stdout.Write(stream.Buffer())
|
|
|
|
// Output:
|
|
|
|
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
|
|
|
}
|
|
|
|
|
2017-07-09 11:24:26 +08:00
|
|
|
func ExampleConfigFastest_Unmarshal() {
|
2017-06-17 17:14:34 +08:00
|
|
|
var jsonBlob = []byte(`[
|
|
|
|
{"Name": "Platypus", "Order": "Monotremata"},
|
|
|
|
{"Name": "Quoll", "Order": "Dasyuromorphia"}
|
|
|
|
]`)
|
|
|
|
type Animal struct {
|
|
|
|
Name string
|
|
|
|
Order string
|
|
|
|
}
|
|
|
|
var animals []Animal
|
2017-07-09 11:10:44 +08:00
|
|
|
iter := ConfigFastest.BorrowIterator(jsonBlob)
|
|
|
|
defer ConfigFastest.ReturnIterator(iter)
|
2017-06-17 17:14:34 +08:00
|
|
|
iter.ReadVal(&animals)
|
|
|
|
if iter.Error != nil {
|
|
|
|
fmt.Println("error:", iter.Error)
|
|
|
|
}
|
|
|
|
fmt.Printf("%+v", animals)
|
|
|
|
// Output:
|
|
|
|
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
2017-06-17 21:11:23 +08:00
|
|
|
}
|
2017-06-18 23:42:23 +08:00
|
|
|
|
2017-07-09 11:24:26 +08:00
|
|
|
func ExampleGet() {
|
2017-06-18 23:42:23 +08:00
|
|
|
val := []byte(`{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`)
|
2017-07-09 11:10:44 +08:00
|
|
|
fmt.Printf(Get(val, "Colors", 0).ToString())
|
2017-06-18 23:42:23 +08:00
|
|
|
// Output:
|
|
|
|
// Crimson
|
2017-06-19 23:43:53 +08:00
|
|
|
}
|