1- package jsoniter
1+ package misc_tests
22
33import (
44 "encoding/json"
55 "github.com/stretchr/testify/require"
66 "strings"
77 "testing"
8+ "github.com/json-iterator/go"
89)
910
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-
2011func Test_jsoniter_RawMessage (t * testing.T ) {
2112 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 ))
2415 should .Equal (`[1,2,3]` , string (data ))
25- str , err := MarshalToString (data )
16+ str , err := jsoniter . MarshalToString (data )
2617 should .Nil (err )
2718 should .Equal (`[1,2,3]` , str )
2819}
2920
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-
6721func Test_encode_map_of_jsoniter_raw_message (t * testing.T ) {
6822 should := require .New (t )
69- type RawMap map [string ]* RawMessage
70- value := RawMessage ("[]" )
23+ type RawMap map [string ]* jsoniter. RawMessage
24+ value := jsoniter . RawMessage ("[]" )
7125 rawMap := RawMap {"hello" : & value }
72- output , err := MarshalToString (rawMap )
26+ output , err := jsoniter . MarshalToString (rawMap )
7327 should .Nil (err )
7428 should .Equal (`{"hello":[]}` , output )
7529}
@@ -82,8 +36,8 @@ func Test_marshal_invalid_json_raw_message(t *testing.T) {
8236
8337 a := A {}
8438 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 )
8741 should .Equal (`{"raw":null}` , string (aout ))
8842 should .Nil (aouterr )
8943}
@@ -99,14 +53,14 @@ func Test_raw_message_memory_not_copied_issue(t *testing.T) {
9953 BiddingMax * float32 `json:"bidding_max"`
10054 BiddingMin * float32 `json:"bidding_min"`
10155 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"`
10559 Speed * int `json:"speed" db:"speed"`
10660 }
10761
10862 obj := & IteratorObject {}
109- decoder := NewDecoder (strings .NewReader (jsonStream ))
63+ decoder := jsoniter . NewDecoder (strings .NewReader (jsonStream ))
11064 err := decoder .Decode (obj )
11165 should := require .New (t )
11266 should .Nil (err )
0 commit comments