@@ -130,16 +130,15 @@ private class DerivedUnionConverterInner<TType, TItem1, TItem2> : JsonConverter<
130130 public override void Write ( Utf8JsonWriter writer , TType value ,
131131 JsonSerializerOptions options )
132132 {
133- if ( value . Item1 is not null )
133+ switch ( value . Tag )
134134 {
135- JsonSerializer . Serialize ( writer , value . Item1 , value . Item1 . GetType ( ) , options ) ;
136- return ;
137- }
135+ case 0 :
136+ JsonSerializer . Serialize ( writer , value . Item1 , value . Item1 ! . GetType ( ) , options ) ;
137+ return ;
138138
139- if ( value . Item2 is not null )
140- {
141- JsonSerializer . Serialize ( writer , value . Item2 , value . Item2 . GetType ( ) , options ) ;
142- return ;
139+ case 1 :
140+ JsonSerializer . Serialize ( writer , value . Item2 , value . Item2 ! . GetType ( ) , options ) ;
141+ return ;
143142 }
144143
145144 throw new JsonException ( "Invalid union type." ) ;
@@ -196,16 +195,15 @@ private class UnionConverterInner<TItem1, TItem2> : JsonConverter<Union<TItem1,
196195 public override void Write ( Utf8JsonWriter writer , Union < TItem1 , TItem2 > value ,
197196 JsonSerializerOptions options )
198197 {
199- if ( value . Item1 is not null )
198+ switch ( value . Tag )
200199 {
201- JsonSerializer . Serialize ( writer , value . Item1 , value . Item1 . GetType ( ) , options ) ;
202- return ;
203- }
200+ case 0 :
201+ JsonSerializer . Serialize ( writer , value . Item1 , value . Item1 ! . GetType ( ) , options ) ;
202+ return ;
204203
205- if ( value . Item2 is not null )
206- {
207- JsonSerializer . Serialize ( writer , value . Item2 , value . Item2 . GetType ( ) , options ) ;
208- return ;
204+ case 1 :
205+ JsonSerializer . Serialize ( writer , value . Item2 , value . Item2 ! . GetType ( ) , options ) ;
206+ return ;
209207 }
210208
211209 throw new JsonException ( "Invalid union type" ) ;
0 commit comments