1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/example_test.go

122 lines
2.6 KiB
Go
Raw Normal View History

2017-07-09 05:10:44 +02:00
package jsoniter
2017-06-05 14:37:08 +02:00
import (
"fmt"
"os"
2018-02-23 12:20:14 +02:00
"strings"
2017-06-05 14:37:08 +02:00
)
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
}
2018-02-23 12:20:14 +02:00
2019-01-10 18:51:25 +02:00
func ExampleMyKey() {
2018-02-23 12:20:14 +02:00
hello := MyKey("hello")
output, _ := Marshal(map[*MyKey]string{&hello: "world"})
fmt.Println(string(output))
obj := map[*MyKey]string{}
Unmarshal(output, &obj)
for k, v := range obj {
fmt.Println(*k, v)
}
// Output:
// {"Hello":"world"}
// Hel world
}
type MyKey string
func (m *MyKey) MarshalText() ([]byte, error) {
return []byte(strings.Replace(string(*m), "h", "H", -1)), nil
}
func (m *MyKey) UnmarshalText(text []byte) error {
*m = MyKey(text[:3])
return nil
}