jsoniter_demo_test.go 696 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func Test_bind_api_demo(t *testing.T) {
  7. iter := ParseString(`[0,1,2,3]`)
  8. val := []int{}
  9. iter.ReadVal(&val)
  10. fmt.Println(val[3])
  11. }
  12. func Test_iterator_api_demo(t *testing.T) {
  13. iter := ParseString(`[0,1,2,3]`)
  14. total := 0
  15. for iter.ReadArray() {
  16. total += iter.ReadInt()
  17. }
  18. fmt.Println(total)
  19. }
  20. type User struct {
  21. userID int
  22. name string
  23. tags []string
  24. }
  25. func Test_iterator_and_bind_api(t *testing.T) {
  26. iter := ParseString(`[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`)
  27. user := User{}
  28. iter.ReadArray()
  29. user.userID = iter.ReadInt()
  30. iter.ReadArray()
  31. iter.ReadVal(&user)
  32. iter.ReadArray() // array end
  33. fmt.Println(user)
  34. }