github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/jsoni/misc_tests/jsoniter_raw_message_test.go (about)

     1  package misc_tests
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/bingoohuang/gg/pkg/jsoni"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func Test_jsoniter_RawMessage(t *testing.T) {
    14  	should := require.New(t)
    15  	var data jsoni.RawMessage
    16  	should.Nil(jsoni.Unmarshal([]byte(`[1,2,3]`), &data))
    17  	should.Equal(`[1,2,3]`, string(data))
    18  	str, err := jsoni.MarshalToString(data)
    19  	should.Nil(err)
    20  	should.Equal(`[1,2,3]`, str)
    21  }
    22  
    23  func Test_encode_map_of_jsoniter_raw_message(t *testing.T) {
    24  	should := require.New(t)
    25  	type RawMap map[string]*jsoni.RawMessage
    26  	value := jsoni.RawMessage("[]")
    27  	rawMap := RawMap{"hello": &value}
    28  	output, err := jsoni.MarshalToString(rawMap)
    29  	should.Nil(err)
    30  	should.Equal(`{"hello":[]}`, output)
    31  }
    32  
    33  func Test_marshal_invalid_json_raw_message(t *testing.T) {
    34  	type A struct {
    35  		Raw json.RawMessage `json:"raw"`
    36  	}
    37  	message := []byte(`{}`)
    38  
    39  	a := A{}
    40  	should := require.New(t)
    41  	should.Nil(jsoni.ConfigCompatibleWithStandardLibrary.Unmarshal(context.Background(), message, &a))
    42  	aout, aouterr := jsoni.ConfigCompatibleWithStandardLibrary.Marshal(context.Background(), &a)
    43  	should.Equal(`{"raw":null}`, string(aout))
    44  	should.Nil(aouterr)
    45  }
    46  
    47  func Test_marshal_nil_json_raw_message(t *testing.T) {
    48  	type A struct {
    49  		Nil1 jsoni.RawMessage `json:"raw1"`
    50  		Nil2 json.RawMessage  `json:"raw2"`
    51  	}
    52  
    53  	a := A{}
    54  	should := require.New(t)
    55  	aout, aouterr := jsoni.Marshal(&a)
    56  	should.Equal(`{"raw1":null,"raw2":null}`, string(aout))
    57  	should.Nil(aouterr)
    58  
    59  	a.Nil1 = []byte(`Any`)
    60  	a.Nil2 = []byte(`Any`)
    61  	should.Nil(jsoni.Unmarshal(aout, &a))
    62  	should.Nil(a.Nil1)
    63  	should.Nil(a.Nil2)
    64  }
    65  
    66  func Test_raw_message_memory_not_copied_issue(t *testing.T) {
    67  	jsonStream := `{"name":"xxxxx","bundle_id":"com.zonst.majiang","app_platform":"ios","app_category":"100103", "budget_day":1000,"bidding_min":1,"bidding_max":2,"bidding_type":"CPM", "freq":{"open":true,"type":"day","num":100},"speed":1, "targeting":{"vendor":{"open":true,"list":["zonst"]}, "geo_code":{"open":true,"list":["156110100"]},"app_category":{"open":true,"list":["100101"]}, "day_parting":{"open":true,"list":["100409","100410"]},"device_type":{"open":true,"list":["ipad"]}, "os_version":{"open":true,"list":[10]},"carrier":{"open":true,"list":["mobile"]}, "network":{"open":true,"list":["4G"]}},"url":{"tracking_imp_url":"http://www.baidu.com", "tracking_clk_url":"http://www.baidu.com","jump_url":"http://www.baidu.com","deep_link_url":"http://www.baidu.com"}}`
    68  	type IteratorObject struct {
    69  		Name        *string           `json:"name"`
    70  		BundleId    *string           `json:"bundle_id"`
    71  		AppCategory *string           `json:"app_category"`
    72  		AppPlatform *string           `json:"app_platform"`
    73  		BudgetDay   *float32          `json:"budget_day"`
    74  		BiddingMax  *float32          `json:"bidding_max"`
    75  		BiddingMin  *float32          `json:"bidding_min"`
    76  		BiddingType *string           `json:"bidding_type"`
    77  		Freq        *jsoni.RawMessage `json:"freq"`
    78  		Targeting   *jsoni.RawMessage `json:"targeting"`
    79  		Url         *jsoni.RawMessage `json:"url"`
    80  		Speed       *int              `json:"speed" db:"speed"`
    81  	}
    82  
    83  	obj := &IteratorObject{}
    84  	decoder := jsoni.NewDecoder(strings.NewReader(jsonStream))
    85  	err := decoder.Decode(context.Background(), obj)
    86  	should := require.New(t)
    87  	should.Nil(err)
    88  	should.Equal(`{"open":true,"type":"day","num":100}`, string(*obj.Freq))
    89  }