@@ -10,7 +10,9 @@ public bool TryAdd(object target, string segment, JsonSerializerOptions options,
10
10
{
11
11
var obj = ( JsonObject ) target ;
12
12
13
- obj [ segment ] = value != null ? JsonSerializer . SerializeToNode ( value , options ) : null ;
13
+ var propertyName = FindPropertyName ( obj , segment , options ) ;
14
+
15
+ obj [ propertyName ] = value != null ? JsonSerializer . SerializeToNode ( value , options ) : null ;
14
16
15
17
errorMessage = null ;
16
18
return true ;
@@ -20,7 +22,9 @@ public bool TryGet(object target, string segment, JsonSerializerOptions options,
20
22
{
21
23
var obj = ( JsonObject ) target ;
22
24
23
- if ( ! obj . TryGetPropertyValue ( segment , out var valueAsToken ) )
25
+ var propertyName = FindPropertyName ( obj , segment , options ) ;
26
+
27
+ if ( ! obj . TryGetPropertyValue ( propertyName , out var valueAsToken ) )
24
28
{
25
29
value = null ;
26
30
errorMessage = Resources . FormatTargetLocationAtPathSegmentNotFound ( segment ) ;
@@ -36,7 +40,9 @@ public bool TryRemove(object target, string segment, JsonSerializerOptions optio
36
40
{
37
41
var obj = ( JsonObject ) target ;
38
42
39
- if ( ! obj . Remove ( segment ) )
43
+ var propertyName = FindPropertyName ( obj , segment , options ) ;
44
+
45
+ if ( ! obj . Remove ( propertyName ) )
40
46
{
41
47
errorMessage = Resources . FormatTargetLocationAtPathSegmentNotFound ( segment ) ;
42
48
return false ;
@@ -50,13 +56,15 @@ public bool TryReplace(object target, string segment, JsonSerializerOptions opti
50
56
{
51
57
var obj = ( JsonObject ) target ;
52
58
53
- if ( obj [ segment ] == null )
59
+ var propertyName = FindPropertyName ( obj , segment , options ) ;
60
+
61
+ if ( obj [ propertyName ] == null )
54
62
{
55
63
errorMessage = Resources . FormatTargetLocationAtPathSegmentNotFound ( segment ) ;
56
64
return false ;
57
65
}
58
66
59
- obj [ segment ] = value != null ? JsonSerializer . SerializeToNode ( value , options ) : null ;
67
+ obj [ propertyName ] = value != null ? JsonSerializer . SerializeToNode ( value , options ) : null ;
60
68
61
69
errorMessage = null ;
62
70
return true ;
@@ -66,7 +74,9 @@ public bool TryTest(object target, string segment, JsonSerializerOptions options
66
74
{
67
75
var obj = ( JsonObject ) target ;
68
76
69
- if ( ! obj . TryGetPropertyValue ( segment , out var currentValue ) )
77
+ var propertyName = FindPropertyName ( obj , segment , options ) ;
78
+
79
+ if ( ! obj . TryGetPropertyValue ( propertyName , out var currentValue ) )
70
80
{
71
81
errorMessage = Resources . FormatTargetLocationAtPathSegmentNotFound ( segment ) ;
72
82
return false ;
@@ -95,7 +105,9 @@ public bool TryTraverse(object target, string segment, JsonSerializerOptions opt
95
105
{
96
106
var obj = ( JsonObject ) target ;
97
107
98
- if ( ! obj . TryGetPropertyValue ( segment , out JsonNode ? nextTargetToken ) )
108
+ var propertyName = FindPropertyName ( obj , segment , options ) ;
109
+
110
+ if ( ! obj . TryGetPropertyValue ( propertyName , out JsonNode ? nextTargetToken ) )
99
111
{
100
112
nextTarget = null ;
101
113
errorMessage = null ;
@@ -106,4 +118,21 @@ public bool TryTraverse(object target, string segment, JsonSerializerOptions opt
106
118
errorMessage = null ;
107
119
return true ;
108
120
}
121
+
122
+ private static string FindPropertyName ( JsonObject ? obj , string segment , JsonSerializerOptions options )
123
+ {
124
+ if ( ! options . PropertyNameCaseInsensitive || obj == null )
125
+ return segment ;
126
+
127
+ if ( obj . ContainsKey ( segment ) )
128
+ return segment ;
129
+
130
+ foreach ( var node in obj )
131
+ {
132
+ if ( string . Equals ( node . Key , segment , StringComparison . OrdinalIgnoreCase ) )
133
+ return node . Key ;
134
+ }
135
+
136
+ return segment ;
137
+ }
109
138
}
0 commit comments