Bladeren bron

#25 make fielding binding case insensitive

Tao Wen 8 jaren geleden
bovenliggende
commit
e5a1e704ad
2 gewijzigde bestanden met toevoegingen van 9 en 3 verwijderingen
  1. 8 2
      feature_iter_object.go
  2. 1 1
      jsoniter_reflect_struct_test.go

+ 8 - 2
feature_iter_object.go

@@ -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)

+ 1 - 1
jsoniter_reflect_struct_test.go

@@ -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)
 }