Skip to content

Commit

Permalink
fix: fixed to not optimize when lower can't handle byte-by-byte. (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
orisano committed Mar 13, 2023
1 parent b68305f commit 2ef15e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions internal/decoder/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2ef15e7

Please sign in to comment.