@@ -25,66 +25,58 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
2525 return ;
2626 }
2727
28- try
28+ // Handle simple or primitive types directly
29+ if ( IsSimpleType ( typeof ( T ) ) )
2930 {
30- // Handle simple or primitive types directly
31- if ( IsSimpleType ( typeof ( T ) ) )
32- {
33- JsonSerializer . Serialize ( writer , value , options ) ;
34- return ;
35- }
31+ JsonSerializer . Serialize ( writer , value , options ) ;
32+ return ;
33+ }
3634
37- // Handle collections
38- if ( value is IEnumerable enumerable && typeof ( T ) != typeof ( string ) )
35+ // Handle collections
36+ if ( value is IEnumerable enumerable && typeof ( T ) != typeof ( string ) )
37+ {
38+ writer . WriteStartArray ( ) ;
39+ foreach ( var item in enumerable )
3940 {
40- writer . WriteStartArray ( ) ;
41- foreach ( var item in enumerable )
42- {
43- if ( item == null )
44- writer . WriteNullValue ( ) ;
45- else
46- JsonSerializer . Serialize ( writer , item , item . GetType ( ) , options ) ;
47- }
48- writer . WriteEndArray ( ) ;
49- return ;
41+ if ( item == null )
42+ writer . WriteNullValue ( ) ;
43+ else
44+ JsonSerializer . Serialize ( writer , item , item . GetType ( ) , options ) ;
5045 }
46+ writer . WriteEndArray ( ) ;
47+ return ;
48+ }
5149
52- // Handle complex objects
53- writer . WriteStartObject ( ) ;
54- foreach ( var property in typeof ( T ) . GetProperties ( BindingFlags . Public | BindingFlags . Instance ) )
55- {
56- if ( ShouldIgnoreProperty ( property ) )
57- continue ;
58-
59- var propName = options . PropertyNamingPolicy ? . ConvertName ( property . Name ) ?? property . Name ;
50+ // Handle complex objects
51+ writer . WriteStartObject ( ) ;
52+ foreach ( var property in typeof ( T ) . GetProperties ( BindingFlags . Public | BindingFlags . Instance ) )
53+ {
54+ if ( ShouldIgnoreProperty ( property ) )
55+ continue ;
6056
61- if ( property . GetIndexParameters ( ) . Length > 0 )
62- continue ; // Skip indexers entirely
57+ var propName = options . PropertyNamingPolicy ? . ConvertName ( property . Name ) ?? property . Name ;
6358
64- object ? propValue ;
65- try
66- {
67- propValue = property . GetValue ( value ) ;
68- }
69- catch
70- {
71- // Fallback: write null if getting property fails
72- propValue = null ;
73- }
59+ if ( property . GetIndexParameters ( ) . Length > 0 )
60+ continue ; // Skip indexers entirely
7461
75- writer . WritePropertyName ( propName ) ;
76- if ( propValue == null )
77- writer . WriteNullValue ( ) ;
78- else
79- JsonSerializer . Serialize ( writer , propValue , propValue . GetType ( ) , options ) ;
62+ object ? propValue ;
63+ try
64+ {
65+ propValue = property . GetValue ( value ) ;
8066 }
81- writer . WriteEndObject ( ) ;
82- }
83- catch
84- {
85- // Fallback to system serializer on complete failure
86- JsonSerializer . Serialize ( writer , value , value . GetType ( ) , options ) ;
67+ catch
68+ {
69+ // Fallback: write null if getting property fails
70+ propValue = null ;
71+ }
72+
73+ writer . WritePropertyName ( propName ) ;
74+ if ( propValue == null )
75+ writer . WriteNullValue ( ) ;
76+ else
77+ JsonSerializer . Serialize ( writer , propValue , propValue . GetType ( ) , options ) ;
8778 }
79+ writer . WriteEndObject ( ) ;
8880 }
8981
9082 private static bool ShouldIgnoreProperty ( PropertyInfo property )
0 commit comments