jsoniter_demo_test.go 1015 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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_any_api_demo(t *testing.T) {
  13. iter := ParseString(`[0,1,2,3]`)
  14. val := iter.ReadAny()
  15. fmt.Println(val.Get(3))
  16. }
  17. func Test_iterator_api_demo(t *testing.T) {
  18. iter := ParseString(`[0,1,2,3]`)
  19. total := 0
  20. for iter.ReadArray() {
  21. total += iter.ReadInt()
  22. }
  23. fmt.Println(total)
  24. }
  25. type ABC struct {
  26. a Any
  27. }
  28. func Test_deep_nested_any_api(t *testing.T) {
  29. iter := ParseString(`{"a": {"b": {"c": "d"}}}`)
  30. abc := &ABC{}
  31. iter.ReadVal(&abc)
  32. fmt.Println(abc.a.Get("b", "c"))
  33. }
  34. type User struct {
  35. userID int
  36. name string
  37. tags []string
  38. }
  39. func Test_iterator_and_bind_api(t *testing.T) {
  40. iter := ParseString(`[123, {"name": "taowen", "tags": ["crazy", "hacker"]}]`)
  41. user := User{}
  42. iter.ReadArray()
  43. user.userID = iter.ReadInt()
  44. iter.ReadArray()
  45. iter.ReadVal(&user)
  46. iter.ReadArray() // array end
  47. fmt.Println(user)
  48. }