jsoniter_struct_encoder_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func Test_encode_unexported_field(t *testing.T) {
  9. type TestData struct {
  10. a int
  11. b <-chan int
  12. C int
  13. d *time.Timer
  14. }
  15. should := require.New(t)
  16. testChan := make(<-chan int, 10)
  17. testTimer := time.NewTimer(10 * time.Second)
  18. obj := &TestData{
  19. a: 42,
  20. b: testChan,
  21. C: 21,
  22. d: testTimer,
  23. }
  24. jb, err := json.Marshal(obj)
  25. should.NoError(err)
  26. should.Equal([]byte(`{"C":21}`), jb)
  27. err = json.Unmarshal([]byte(`{"a": 444, "b":"bad", "C":55, "d":{"not": "a timer"}}`), obj)
  28. should.NoError(err)
  29. should.Equal(42, obj.a)
  30. should.Equal(testChan, obj.b)
  31. should.Equal(55, obj.C)
  32. should.Equal(testTimer, obj.d)
  33. jb, err = Marshal(obj)
  34. should.NoError(err)
  35. should.Equal(jb, []byte(`{"C":55}`))
  36. err = Unmarshal([]byte(`{"a": 444, "b":"bad", "C":256, "d":{"not":"a timer"}}`), obj)
  37. should.NoError(err)
  38. should.Equal(42, obj.a)
  39. should.Equal(testChan, obj.b)
  40. should.Equal(256, obj.C)
  41. should.Equal(testTimer, obj.d)
  42. }