@@ -178,6 +178,11 @@ private static (object?, string?) TryConvertObject(Action<string> warn, string c
178
178
if ( targetType . GetGenericTypeDefinition ( ) == typeof ( ImmutableDictionary < , > ) )
179
179
return TryConvertDictionary ( warn , context , val , targetType ) ;
180
180
181
+ if ( targetType . GetGenericTypeDefinition ( ) == typeof ( Output < > ) )
182
+ return ( ( object ? , string ? ) ) TryConvertOutputMethod
183
+ . MakeGenericMethod ( targetType . GetGenericArguments ( ) . Single ( ) )
184
+ . Invoke ( null , new object ? [ ] { warn , context , val , targetType } ) ! ;
185
+
181
186
throw new InvalidOperationException (
182
187
$ "Unexpected generic target type { targetType . FullName } when deserializing { context } ") ;
183
188
}
@@ -272,6 +277,9 @@ private static (object?, string?) TryConvertJsonElement(
272
277
}
273
278
writer . WriteEndObject ( ) ;
274
279
return null ;
280
+ case IOutput output :
281
+ var outputData = output . GetDataAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
282
+ return TryWriteJson ( context , writer , outputData . Value ) ;
275
283
default :
276
284
return $ "Unexpected type { val . GetType ( ) . FullName } when converting { context } to { nameof ( JsonElement ) } ";
277
285
}
@@ -329,6 +337,23 @@ private static (object?, string?) TryConvertArray(
329
337
return ( builderToImmutable . Invoke ( builder , null ) , null ) ;
330
338
}
331
339
340
+ private static readonly MethodInfo TryConvertOutputMethod = typeof ( Converter ) . GetMethod ( nameof ( TryConvertOutput ) , BindingFlags . NonPublic | BindingFlags . Static )
341
+ ?? throw new MissingMethodException ( $ "Method { nameof ( TryConvertOutput ) } not found.") ;
342
+
343
+ private static ( object ? , string ? ) TryConvertOutput < T > (
344
+ Action < string > warn ,
345
+ string fieldName , object val , Type targetType )
346
+ {
347
+ var ( element , error ) = TryConvertObject ( warn , fieldName , val , typeof ( T ) ) ;
348
+
349
+ if ( error != null )
350
+ {
351
+ return ( element , error ) ;
352
+ }
353
+
354
+ return ( Output . Create ( ( T ) element ! ) , null ) ;
355
+ }
356
+
332
357
private static ( object ? , string ? ) TryConvertDictionary (
333
358
Action < string > warn ,
334
359
string fieldName , object val , Type targetType )
@@ -471,6 +496,11 @@ static bool CheckEnumType(Type targetType, Type underlyingType)
471
496
CheckTargetType ( context , dictTypeArgs [ 1 ] , seenTypes ) ;
472
497
return ;
473
498
}
499
+ if ( targetType . GetGenericTypeDefinition ( ) == typeof ( Output < > ) )
500
+ {
501
+ CheckTargetType ( context , targetType . GenericTypeArguments . Single ( ) , seenTypes ) ;
502
+ return ;
503
+ }
474
504
throw new InvalidOperationException ( $@ "{ context } contains invalid type { targetType . FullName } :
475
505
The only generic types allowed are ImmutableArray<...> and ImmutableDictionary<string, ...>" ) ;
476
506
}
0 commit comments