@@ -10,45 +10,42 @@ namespace SmartImage.Lib.Utilities;
1010#pragma warning disable CS0649
1111public class NonPublicMembersConverter < T > : JsonConverter < T > where T : class
1212{
13- public override T Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
14- {
15- T instance = ( T ) Activator . CreateInstance ( typeToConvert , nonPublic : true ) ;
16-
17- while ( reader . Read ( ) )
18- {
19- if ( reader . TokenType == JsonTokenType . EndObject )
20- {
21- break ;
22- }
23-
24- if ( reader . TokenType != JsonTokenType . PropertyName )
25- {
26- throw new JsonException ( ) ;
27- }
28-
29- string propertyName = reader . GetString ( ) ;
30-
31- PropertyInfo propertyInfo =
32- typeToConvert . GetProperty ( propertyName ,
33- BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
34-
35- if ( propertyInfo != null && propertyInfo . CanWrite )
36- {
37- reader . Read ( ) ; // Move to the property value
38- object value = JsonSerializer . Deserialize ( ref reader , propertyInfo . PropertyType , options ) ;
39- propertyInfo . SetValue ( instance , value ) ;
40- }
41- else
42- {
43- reader . Skip ( ) ;
44- }
45- }
46-
47- return instance ;
48- }
49-
50- public override void Write ( Utf8JsonWriter writer , T value , JsonSerializerOptions options )
51- {
52- JsonSerializer . Serialize ( writer , value , options ) ;
53- }
13+
14+ public override T Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
15+ {
16+ T instance = ( T ) Activator . CreateInstance ( typeToConvert , nonPublic : true ) ;
17+
18+ while ( reader . Read ( ) ) {
19+ if ( reader . TokenType == JsonTokenType . EndObject ) {
20+ break ;
21+ }
22+
23+ if ( reader . TokenType != JsonTokenType . PropertyName ) {
24+ throw new JsonException ( ) ;
25+ }
26+
27+ string propertyName = reader . GetString ( ) ;
28+
29+ PropertyInfo propertyInfo =
30+ typeToConvert . GetProperty ( propertyName ,
31+ BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ) ;
32+
33+ if ( propertyInfo != null && propertyInfo . CanWrite ) {
34+ reader . Read ( ) ; // Move to the property value
35+ object value = JsonSerializer . Deserialize ( ref reader , propertyInfo . PropertyType , options ) ;
36+ propertyInfo . SetValue ( instance , value ) ;
37+ }
38+ else {
39+ reader . Skip ( ) ;
40+ }
41+ }
42+
43+ return instance ;
44+ }
45+
46+ public override void Write ( Utf8JsonWriter writer , T value , JsonSerializerOptions options )
47+ {
48+ JsonSerializer . Serialize ( writer , value , options ) ;
49+ }
50+
5451}
0 commit comments