jsoniter_interface_test.go 937 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package jsoniter
  2. import (
  3. "testing"
  4. "github.com/json-iterator/go/require"
  5. )
  6. func Test_write_array_of_interface(t *testing.T) {
  7. should := require.New(t)
  8. array := []interface{}{"hello"}
  9. str, err := MarshalToString(array)
  10. should.Nil(err)
  11. should.Equal(`["hello"]`, str)
  12. }
  13. func Test_write_map_of_interface(t *testing.T) {
  14. should := require.New(t)
  15. val := map[string]interface{}{"hello":"world"}
  16. str, err := MarshalToString(val)
  17. should.Nil(err)
  18. should.Equal(`{"hello":"world"}`, str)
  19. }
  20. type MyInterface interface {
  21. }
  22. func Test_write_map_of_custom_interface(t *testing.T) {
  23. should := require.New(t)
  24. val := map[string]MyInterface{"hello":"world"}
  25. str, err := MarshalToString(val)
  26. should.Nil(err)
  27. should.Equal(`{"hello":"world"}`, str)
  28. }
  29. func Test_write_interface(t *testing.T) {
  30. should := require.New(t)
  31. var val interface{}
  32. val = "hello"
  33. str, err := MarshalToString(val)
  34. should.Nil(err)
  35. should.Equal(`"hello"`, str)
  36. }