mirror of
https://github.com/json-iterator/go.git
synced 2024-11-24 08:22:14 +02:00
91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
package jsoniter_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/json-iterator/go"
|
|
)
|
|
|
|
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 := jsoniter.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 := jsoniter.Unmarshal(jsonBlob, &animals)
|
|
if err != nil {
|
|
fmt.Println("error:", err)
|
|
}
|
|
fmt.Printf("%+v", animals)
|
|
// Output:
|
|
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
|
|
}
|
|
|
|
func ExampleMarshalWithBestPerformance() {
|
|
type ColorGroup struct {
|
|
ID int
|
|
Name string
|
|
Colors []string
|
|
}
|
|
group := ColorGroup{
|
|
ID: 1,
|
|
Name: "Reds",
|
|
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
|
|
}
|
|
stream := jsoniter.ConfigFastest.BorrowStream(nil)
|
|
defer jsoniter.ConfigFastest.ReturnStream(stream)
|
|
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"]}
|
|
}
|
|
|
|
func ExampleUnmarshalWithBestPerformance() {
|
|
var jsonBlob = []byte(`[
|
|
{"Name": "Platypus", "Order": "Monotremata"},
|
|
{"Name": "Quoll", "Order": "Dasyuromorphia"}
|
|
]`)
|
|
type Animal struct {
|
|
Name string
|
|
Order string
|
|
}
|
|
var animals []Animal
|
|
iter := jsoniter.ConfigFastest.BorrowIterator(jsonBlob)
|
|
defer jsoniter.ConfigFastest.ReturnIterator(iter)
|
|
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}]
|
|
}
|