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