@@ -130,16 +130,15 @@ private class DerivedUnionConverterInner<TType, TItem1, TItem2> : JsonConverter<
130
130
public override void Write ( Utf8JsonWriter writer , TType value ,
131
131
JsonSerializerOptions options )
132
132
{
133
- if ( value . Item1 is not null )
133
+ switch ( value . Tag )
134
134
{
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 ;
138
138
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 ;
143
142
}
144
143
145
144
throw new JsonException ( "Invalid union type." ) ;
@@ -196,16 +195,15 @@ private class UnionConverterInner<TItem1, TItem2> : JsonConverter<Union<TItem1,
196
195
public override void Write ( Utf8JsonWriter writer , Union < TItem1 , TItem2 > value ,
197
196
JsonSerializerOptions options )
198
197
{
199
- if ( value . Item1 is not null )
198
+ switch ( value . Tag )
200
199
{
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 ;
204
203
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 ;
209
207
}
210
208
211
209
throw new JsonException ( "Invalid union type" ) ;
0 commit comments