github.com/bytedance/go-tagexpr@v2.7.5-0.20210114074101-de5b8743ad85+incompatible/binding/gjson/gjson_test.go (about)

     1  package gjson
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestMap(t *testing.T) {
    11  	type X struct {
    12  		M1 map[string]interface{}
    13  		M2 map[string]struct {
    14  			A string
    15  			B int
    16  		}
    17  		M3 map[string]*struct {
    18  			A string
    19  			B int
    20  		}
    21  	}
    22  	x := X{
    23  		M1: map[string]interface{}{"i": float64(9), "j": "*"},
    24  		M2: map[string]struct {
    25  			A string
    26  			B int
    27  		}{"k2": {"a2", 12}},
    28  		M3: map[string]*struct {
    29  			A string
    30  			B int
    31  		}{"k3": {"a2", 13}},
    32  	}
    33  	data, _ := json.MarshalIndent(x, "", "  ")
    34  	t.Log(string(data))
    35  
    36  	var x2 X
    37  
    38  	err := unmarshal(data, &x2)
    39  	assert.NoError(t, err)
    40  	assert.Equal(t, x, x2)
    41  
    42  	data = []byte(`{
    43            "M1": {
    44              "i": 9,
    45              "j": "*"
    46            },
    47            "M2": {
    48              "k2": {
    49                "A": "a2",
    50                "B": 12
    51              }
    52            },
    53            "M3": {
    54              "k3": {
    55                "A": "a2",
    56                "B": "13"
    57              }
    58            }
    59          }`)
    60  
    61  	x3 := X{}
    62  	err = unmarshal(data, &x3)
    63  	assert.NoError(t, err)
    64  	assert.Equal(t, x, x3)
    65  }