@@ -2,10 +2,11 @@ package jsoniter
2
2
3
3
import (
4
4
"fmt"
5
- "github.com/modern-go/reflect2"
6
5
"io"
7
6
"strings"
8
7
"unsafe"
8
+
9
+ "github.com/modern-go/reflect2"
9
10
)
10
11
11
12
func decoderOfStruct (ctx * ctx , typ reflect2.Type ) ValDecoder {
@@ -31,11 +32,15 @@ func decoderOfStruct(ctx *ctx, typ reflect2.Type) ValDecoder {
31
32
for k , binding := range bindings {
32
33
fields [k ] = binding .Decoder .(* structFieldDecoder )
33
34
}
34
- for k , binding := range bindings {
35
- if _ , found := fields [strings .ToLower (k )]; ! found {
36
- fields [strings .ToLower (k )] = binding .Decoder .(* structFieldDecoder )
35
+
36
+ if ! ctx .caseSensitive () {
37
+ for k , binding := range bindings {
38
+ if _ , found := fields [strings .ToLower (k )]; ! found {
39
+ fields [strings .ToLower (k )] = binding .Decoder .(* structFieldDecoder )
40
+ }
37
41
}
38
42
}
43
+
39
44
return createStructDecoder (ctx , typ , fields )
40
45
}
41
46
@@ -46,12 +51,13 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
46
51
knownHash := map [int64 ]struct {}{
47
52
0 : {},
48
53
}
54
+
49
55
switch len (fields ) {
50
56
case 0 :
51
57
return & skipObjectDecoder {typ }
52
58
case 1 :
53
59
for fieldName , fieldDecoder := range fields {
54
- fieldHash := calcHash (fieldName )
60
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
55
61
_ , known := knownHash [fieldHash ]
56
62
if known {
57
63
return & generalStructDecoder {typ , fields , false }
@@ -65,7 +71,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
65
71
var fieldDecoder1 * structFieldDecoder
66
72
var fieldDecoder2 * structFieldDecoder
67
73
for fieldName , fieldDecoder := range fields {
68
- fieldHash := calcHash (fieldName )
74
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
69
75
_ , known := knownHash [fieldHash ]
70
76
if known {
71
77
return & generalStructDecoder {typ , fields , false }
@@ -88,7 +94,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
88
94
var fieldDecoder2 * structFieldDecoder
89
95
var fieldDecoder3 * structFieldDecoder
90
96
for fieldName , fieldDecoder := range fields {
91
- fieldHash := calcHash (fieldName )
97
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
92
98
_ , known := knownHash [fieldHash ]
93
99
if known {
94
100
return & generalStructDecoder {typ , fields , false }
@@ -119,7 +125,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
119
125
var fieldDecoder3 * structFieldDecoder
120
126
var fieldDecoder4 * structFieldDecoder
121
127
for fieldName , fieldDecoder := range fields {
122
- fieldHash := calcHash (fieldName )
128
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
123
129
_ , known := knownHash [fieldHash ]
124
130
if known {
125
131
return & generalStructDecoder {typ , fields , false }
@@ -156,7 +162,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
156
162
var fieldDecoder4 * structFieldDecoder
157
163
var fieldDecoder5 * structFieldDecoder
158
164
for fieldName , fieldDecoder := range fields {
159
- fieldHash := calcHash (fieldName )
165
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
160
166
_ , known := knownHash [fieldHash ]
161
167
if known {
162
168
return & generalStructDecoder {typ , fields , false }
@@ -199,7 +205,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
199
205
var fieldDecoder5 * structFieldDecoder
200
206
var fieldDecoder6 * structFieldDecoder
201
207
for fieldName , fieldDecoder := range fields {
202
- fieldHash := calcHash (fieldName )
208
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
203
209
_ , known := knownHash [fieldHash ]
204
210
if known {
205
211
return & generalStructDecoder {typ , fields , false }
@@ -248,7 +254,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
248
254
var fieldDecoder6 * structFieldDecoder
249
255
var fieldDecoder7 * structFieldDecoder
250
256
for fieldName , fieldDecoder := range fields {
251
- fieldHash := calcHash (fieldName )
257
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
252
258
_ , known := knownHash [fieldHash ]
253
259
if known {
254
260
return & generalStructDecoder {typ , fields , false }
@@ -303,7 +309,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
303
309
var fieldDecoder7 * structFieldDecoder
304
310
var fieldDecoder8 * structFieldDecoder
305
311
for fieldName , fieldDecoder := range fields {
306
- fieldHash := calcHash (fieldName )
312
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
307
313
_ , known := knownHash [fieldHash ]
308
314
if known {
309
315
return & generalStructDecoder {typ , fields , false }
@@ -364,7 +370,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
364
370
var fieldDecoder8 * structFieldDecoder
365
371
var fieldDecoder9 * structFieldDecoder
366
372
for fieldName , fieldDecoder := range fields {
367
- fieldHash := calcHash (fieldName )
373
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
368
374
_ , known := knownHash [fieldHash ]
369
375
if known {
370
376
return & generalStructDecoder {typ , fields , false }
@@ -431,7 +437,7 @@ func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structF
431
437
var fieldDecoder9 * structFieldDecoder
432
438
var fieldDecoder10 * structFieldDecoder
433
439
for fieldName , fieldDecoder := range fields {
434
- fieldHash := calcHash (fieldName )
440
+ fieldHash := calcHash (fieldName , ctx . caseSensitive () )
435
441
_ , known := knownHash [fieldHash ]
436
442
if known {
437
443
return & generalStructDecoder {typ , fields , false }
@@ -513,13 +519,13 @@ func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *It
513
519
fieldBytes := iter .ReadStringAsSlice ()
514
520
field = * (* string )(unsafe .Pointer (& fieldBytes ))
515
521
fieldDecoder = decoder .fields [field ]
516
- if fieldDecoder == nil {
522
+ if fieldDecoder == nil && ! iter . cfg . caseSensitive {
517
523
fieldDecoder = decoder .fields [strings .ToLower (field )]
518
524
}
519
525
} else {
520
526
field = iter .ReadString ()
521
527
fieldDecoder = decoder .fields [field ]
522
- if fieldDecoder == nil {
528
+ if fieldDecoder == nil && ! iter . cfg . caseSensitive {
523
529
fieldDecoder = decoder .fields [strings .ToLower (field )]
524
530
}
525
531
}
0 commit comments