現(xiàn)象
type X struct {
Data string `json:"data"`
}
func main() {
var b = []byte(`{"DATA":"xxx"}`)
x := &X{}
_ = json.Unmarshal(b, x)
fmt.Println(x) // 輸出: &{xxx}
}
疑問
json tag
不相等诡曙,即 data != DATA
為啥還可以反序列化成功呢由桌?
源碼
go/src/encoding/json/fold.go:foldFunc
// foldFunc returns one of four different case folding equivalence
// functions, from most general (and slow) to fastest:
//
// 1) bytes.EqualFold, if the key s contains any non-ASCII UTF-8
// 2) equalFoldRight, if s contains special folding ASCII ('k', 'K', 's', 'S')
// 3) asciiEqualFold, no special, but includes non-letters (including _)
// 4) simpleLetterEqualFold, no specials, no non-letters.
//
// The letters S and K are special because they map to 3 runes, not just 2:
// * S maps to s and to U+017F '?' Latin small letter long s
// * k maps to K and to U+212A '?' Kelvin sign
// See https://play.golang.org/p/tTxjOc0OGo
//
// The returned function is specialized for matching against s and
// should only be given s. It's not curried for performance reasons.
func foldFunc(s []byte) func(s, t []byte) bool {
nonLetter := false
special := false // special letter
for _, b := range s {
if b >= utf8.RuneSelf {
return bytes.EqualFold
}
upper := b & caseMask
if upper < 'A' || upper > 'Z' {
nonLetter = true
} else if upper == 'K' || upper == 'S' {
// See above for why these letters are special.
special = true
}
}
if special {
return equalFoldRight
}
if nonLetter {
return asciiEqualFold
}
return simpleLetterEqualFold
}
看注釋還是挺容易理解的膊毁,主要分 4 種匹配場景牵舵。
然后在 decode.go
文件的 func (d *decodeState) object(v reflect.Value) error
函數(shù)绰姻。
如果 tag
相等邏輯繼續(xù)凰慈,否則通過 foldFunc
判斷菱属。
var f *field
if i, ok := fields.nameIndex[string(key)]; ok {
// Found an exact name match.
f = &fields.list[i]
} else {
// Fall back to the expensive case-insensitive
// linear search.
for i := range fields.list {
ff := &fields.list[i]
if ff.equalFold(ff.nameBytes, key) {
f = ff
break
}
}
}
結(jié)果
嗯钳榨,所以出現(xiàn)了上述的問題。