jsoniter_any_string_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package any_tests
  2. import (
  3. "testing"
  4. "github.com/json-iterator/go"
  5. "github.com/stretchr/testify/require"
  6. )
  7. var stringConvertMap = map[string]string{
  8. "null": "",
  9. "321.1": "321.1",
  10. `"1.1"`: "1.1",
  11. `"-123.1"`: "-123.1",
  12. "0.0": "0.0",
  13. "0": "0",
  14. `"0"`: "0",
  15. `"0.0"`: "0.0",
  16. `"00.0"`: "00.0",
  17. "true": "true",
  18. "false": "false",
  19. `"true"`: "true",
  20. `"false"`: "false",
  21. `"true123"`: "true123",
  22. `"+1"`: "+1",
  23. "[]": "[]",
  24. "[1,2]": "[1,2]",
  25. "{}": "{}",
  26. `{"a":1, "stream":true}`: `{"a":1, "stream":true}`,
  27. }
  28. func Test_read_any_to_string(t *testing.T) {
  29. should := require.New(t)
  30. for k, v := range stringConvertMap {
  31. any := jsoniter.Get([]byte(k))
  32. should.Equal(v, any.ToString(), "original val "+k)
  33. }
  34. }
  35. func Test_read_string_as_any(t *testing.T) {
  36. should := require.New(t)
  37. any := jsoniter.Get([]byte(`"hello"`))
  38. should.Equal("hello", any.ToString())
  39. should.True(any.ToBool())
  40. any = jsoniter.Get([]byte(`" "`))
  41. should.False(any.ToBool())
  42. any = jsoniter.Get([]byte(`"false"`))
  43. should.True(any.ToBool())
  44. any = jsoniter.Get([]byte(`"123"`))
  45. should.Equal(123, any.ToInt())
  46. }
  47. func Test_wrap_string(t *testing.T) {
  48. should := require.New(t)
  49. any := jsoniter.Get([]byte("-32000")).MustBeValid()
  50. should.Equal(-32000, any.ToInt())
  51. should.NoError(any.LastError())
  52. }