1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00

#25 make fielding binding case insensitive

This commit is contained in:
Tao Wen 2017-05-24 13:16:09 +08:00
parent 7d5f90261e
commit e5a1e704ad
2 changed files with 9 additions and 3 deletions

View File

@ -1,6 +1,9 @@
package jsoniter
import "fmt"
import (
"fmt"
"unicode"
)
func (iter *Iterator) ReadObject() (ret string) {
c := iter.nextToken()
@ -37,6 +40,9 @@ func (iter *Iterator) readFieldHash() int32 {
for i := iter.head; i < iter.tail; i++ {
// require ascii string and no escape
b := iter.buf[i]
if 'A' <= b && b <= 'Z' {
b += 'a' - 'A'
}
if b == '"' {
iter.head = i+1
c = iter.nextToken()
@ -61,7 +67,7 @@ func (iter *Iterator) readFieldHash() int32 {
func calcHash(str string) int32 {
hash := int64(0x811c9dc5)
for _, b := range str {
hash ^= int64(b)
hash ^= int64(unicode.ToLower(b))
hash *= 0x1000193
}
return int32(hash)

View File

@ -14,7 +14,7 @@ func Test_decode_one_field_struct(t *testing.T) {
obj := TestObject{}
should.Nil(UnmarshalFromString(`{}`, &obj))
should.Equal("", obj.Field1)
should.Nil(UnmarshalFromString(`{"Field1": "hello"}`, &obj))
should.Nil(UnmarshalFromString(`{"field1": "hello"}`, &obj))
should.Equal("hello", obj.Field1)
}