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

     1  package misc_tests
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"math/big"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/bingoohuang/gg/pkg/jsoni"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func Test_decode_TextMarshaler_key_map(t *testing.T) {
    15  	should := require.New(t)
    16  	var val map[*big.Float]string
    17  	should.Nil(jsoni.UnmarshalFromString(`{"1":"2"}`, &val))
    18  	str, err := jsoni.MarshalToString(val)
    19  	should.Nil(err)
    20  	should.Equal(`{"1":"2"}`, str)
    21  }
    22  
    23  func Test_read_map_with_reader(t *testing.T) {
    24  	should := require.New(t)
    25  	input := `{"branch":"beta","change_log":"add the rows{10}","channel":"fros","create_time":"2017-06-13 16:39:08","firmware_list":"","md5":"80dee2bf7305bcf179582088e29fd7b9","note":{"CoreServices":{"md5":"d26975c0a8c7369f70ed699f2855cc2e","package_name":"CoreServices","version_code":"76","version_name":"1.0.76"},"FrDaemon":{"md5":"6b1f0626673200bc2157422cd2103f5d","package_name":"FrDaemon","version_code":"390","version_name":"1.0.390"},"FrGallery":{"md5":"90d767f0f31bcd3c1d27281ec979ba65","package_name":"FrGallery","version_code":"349","version_name":"1.0.349"},"FrLocal":{"md5":"f15a215b2c070a80a01f07bde4f219eb","package_name":"FrLocal","version_code":"791","version_name":"1.0.791"}},"pack_region_urls":{"CN":"https://s3.cn-north-1.amazonaws.com.cn/xxx-os/ttt_xxx_android_1.5.3.344.393.zip","default":"http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip","local":"http://192.168.8.78/ttt_xxx_android_1.5.3.344.393.zip"},"pack_version":"1.5.3.344.393","pack_version_code":393,"region":"all","release_flag":0,"revision":62,"size":38966875,"status":3}`
    26  	reader := strings.NewReader(input)
    27  	decoder := jsoni.ConfigCompatibleWithStandardLibrary.NewDecoder(reader)
    28  	m1 := map[string]interface{}{}
    29  	should.Nil(decoder.Decode(context.Background(), &m1))
    30  	m2 := map[string]interface{}{}
    31  	should.Nil(json.Unmarshal([]byte(input), &m2))
    32  	should.Equal(m2, m1)
    33  	should.Equal("1.0.76", m1["note"].(map[string]interface{})["CoreServices"].(map[string]interface{})["version_name"])
    34  }
    35  
    36  func Test_map_eface_of_eface(t *testing.T) {
    37  	should := require.New(t)
    38  	json := jsoni.ConfigCompatibleWithStandardLibrary
    39  	output, err := json.MarshalToString(context.Background(), map[interface{}]interface{}{
    40  		"1": 2,
    41  		3:   "4",
    42  	})
    43  	should.NoError(err)
    44  	should.Equal(`{"1":2,"3":"4"}`, output)
    45  }
    46  
    47  func Test_encode_nil_map(t *testing.T) {
    48  	should := require.New(t)
    49  	var nilMap map[string]string
    50  	output, err := jsoni.MarshalToString(nilMap)
    51  	should.NoError(err)
    52  	should.Equal(`null`, output)
    53  }