fix: fixed to not optimize when lower can't handle byte-by-byte. (#432)

This commit is contained in:
Nao Yonashiro 2023-03-13 19:57:24 +09:00 committed by GitHub
parent b68305f5d1
commit 2ef15e72f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -4016,6 +4016,18 @@ func TestIssue408(t *testing.T) {
}
}
func TestIssue416(t *testing.T) {
b := []byte(`{"Сообщение":"Текст"}`)
type T struct {
Msg string `json:"Сообщение"`
}
var x T
err := json.Unmarshal(b, &x)
assertErr(t, err)
assertEq(t, "unexpected result", "Текст", x.Msg)
}
func TestIssue429(t *testing.T) {
var x struct {
N int32

View File

@ -51,6 +51,14 @@ func init() {
}
}
func toASCIILower(s string) string {
b := []byte(s)
for i := range b {
b[i] = largeToSmallTable[b[i]]
}
return string(b)
}
func newStructDecoder(structName, fieldName string, fieldMap map[string]*structFieldSet) *structDecoder {
return &structDecoder{
fieldMap: fieldMap,
@ -91,6 +99,10 @@ func (d *structDecoder) tryOptimize() {
for k, v := range d.fieldMap {
key := strings.ToLower(k)
if key != k {
if key != toASCIILower(k) {
d.isTriedOptimize = true
return
}
// already exists same key (e.g. Hello and HELLO has same lower case key
if _, exists := conflicted[key]; exists {
d.isTriedOptimize = true