1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-23 18:54:21 +02:00
json-iterator/jsoniter_demo_test.go

39 lines
695 B
Go
Raw Normal View History

2016-12-11 15:53:35 +08:00
package jsoniter
import (
"fmt"
"testing"
2016-12-11 15:53:35 +08:00
)
func Test_bind_api_demo(t *testing.T) {
iter := ParseString(`[0,1,2,3]`)
val := []int{}
2017-01-09 17:47:21 +08:00
iter.ReadVal(&val)
2016-12-11 15:53:35 +08:00
fmt.Println(val[3])
}
func Test_iterator_api_demo(t *testing.T) {
iter := ParseString(`[0,1,2,3]`)
total := 0
for iter.ReadArray() {
total += iter.ReadInt()
}
fmt.Println(total)
}
type User struct {
userID int
name string
tags []string
2016-12-11 15:53:35 +08:00
}
func Test_iterator_and_bind_api(t *testing.T) {
iter := ParseString(`[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`)
user := User{}
iter.ReadArray()
user.userID = iter.ReadInt()
2016-12-11 15:53:35 +08:00
iter.ReadArray()
2017-01-09 17:47:21 +08:00
iter.ReadVal(&user)
2016-12-11 15:53:35 +08:00
iter.ReadArray() // array end
fmt.Println(user)
2017-02-03 18:44:54 +08:00
}