1
- package jsoniter
1
+ package misc_tests
2
2
3
3
import (
4
4
"encoding/json"
5
5
"github.com/stretchr/testify/require"
6
6
"strings"
7
7
"testing"
8
+ "github.com/json-iterator/go"
8
9
)
9
10
10
- func Test_json_RawMessage (t * testing.T ) {
11
- should := require .New (t )
12
- var data json.RawMessage
13
- should .Nil (Unmarshal ([]byte (`[1,2,3]` ), & data ))
14
- should .Equal (`[1,2,3]` , string (data ))
15
- str , err := MarshalToString (data )
16
- should .Nil (err )
17
- should .Equal (`[1,2,3]` , str )
18
- }
19
-
20
11
func Test_jsoniter_RawMessage (t * testing.T ) {
21
12
should := require .New (t )
22
- var data RawMessage
23
- should .Nil (Unmarshal ([]byte (`[1,2,3]` ), & data ))
13
+ var data jsoniter. RawMessage
14
+ should .Nil (jsoniter . Unmarshal ([]byte (`[1,2,3]` ), & data ))
24
15
should .Equal (`[1,2,3]` , string (data ))
25
- str , err := MarshalToString (data )
16
+ str , err := jsoniter . MarshalToString (data )
26
17
should .Nil (err )
27
18
should .Equal (`[1,2,3]` , str )
28
19
}
29
20
30
- func Test_json_RawMessage_in_struct (t * testing.T ) {
31
- type TestObject struct {
32
- Field1 string
33
- Field2 json.RawMessage
34
- }
35
- should := require .New (t )
36
- var data TestObject
37
- should .Nil (Unmarshal ([]byte (`{"field1": "hello", "field2": [1,2,3]}` ), & data ))
38
- should .Equal (` [1,2,3]` , string (data .Field2 ))
39
- should .Equal (`hello` , data .Field1 )
40
- }
41
-
42
- func Test_decode_map_of_raw_message (t * testing.T ) {
43
- should := require .New (t )
44
- type RawMap map [string ]* json.RawMessage
45
- b := []byte ("{\" test\" :[{\" key\" :\" value\" }]}" )
46
- var rawMap RawMap
47
- should .Nil (Unmarshal (b , & rawMap ))
48
- should .Equal (`[{"key":"value"}]` , string (* rawMap ["test" ]))
49
- type Inner struct {
50
- Key string `json:"key"`
51
- }
52
- var inner []Inner
53
- Unmarshal (* rawMap ["test" ], & inner )
54
- should .Equal ("value" , inner [0 ].Key )
55
- }
56
-
57
- func Test_encode_map_of_raw_message (t * testing.T ) {
58
- should := require .New (t )
59
- type RawMap map [string ]* json.RawMessage
60
- value := json .RawMessage ("[]" )
61
- rawMap := RawMap {"hello" : & value }
62
- output , err := MarshalToString (rawMap )
63
- should .Nil (err )
64
- should .Equal (`{"hello":[]}` , output )
65
- }
66
-
67
21
func Test_encode_map_of_jsoniter_raw_message (t * testing.T ) {
68
22
should := require .New (t )
69
- type RawMap map [string ]* RawMessage
70
- value := RawMessage ("[]" )
23
+ type RawMap map [string ]* jsoniter. RawMessage
24
+ value := jsoniter . RawMessage ("[]" )
71
25
rawMap := RawMap {"hello" : & value }
72
- output , err := MarshalToString (rawMap )
26
+ output , err := jsoniter . MarshalToString (rawMap )
73
27
should .Nil (err )
74
28
should .Equal (`{"hello":[]}` , output )
75
29
}
@@ -82,8 +36,8 @@ func Test_marshal_invalid_json_raw_message(t *testing.T) {
82
36
83
37
a := A {}
84
38
should := require .New (t )
85
- should .Nil (ConfigCompatibleWithStandardLibrary .Unmarshal (message , & a ))
86
- aout , aouterr := ConfigCompatibleWithStandardLibrary .Marshal (& a )
39
+ should .Nil (jsoniter . ConfigCompatibleWithStandardLibrary .Unmarshal (message , & a ))
40
+ aout , aouterr := jsoniter . ConfigCompatibleWithStandardLibrary .Marshal (& a )
87
41
should .Equal (`{"raw":null}` , string (aout ))
88
42
should .Nil (aouterr )
89
43
}
@@ -99,14 +53,14 @@ func Test_raw_message_memory_not_copied_issue(t *testing.T) {
99
53
BiddingMax * float32 `json:"bidding_max"`
100
54
BiddingMin * float32 `json:"bidding_min"`
101
55
BiddingType * string `json:"bidding_type"`
102
- Freq * RawMessage `json:"freq"`
103
- Targeting * RawMessage `json:"targeting"`
104
- Url * RawMessage `json:"url"`
56
+ Freq * jsoniter. RawMessage `json:"freq"`
57
+ Targeting * jsoniter. RawMessage `json:"targeting"`
58
+ Url * jsoniter. RawMessage `json:"url"`
105
59
Speed * int `json:"speed" db:"speed"`
106
60
}
107
61
108
62
obj := & IteratorObject {}
109
- decoder := NewDecoder (strings .NewReader (jsonStream ))
63
+ decoder := jsoniter . NewDecoder (strings .NewReader (jsonStream ))
110
64
err := decoder .Decode (obj )
111
65
should := require .New (t )
112
66
should .Nil (err )
0 commit comments