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