| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | namespace System.Text.Json.Serialization.Metadata |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Contains utilities and combinators acting on <see cref="IJsonTypeInfoResolver"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class JsonTypeInfoResolver |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Combines multiple <see cref="IJsonTypeInfoResolver"/> sources into one. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="resolvers">Sequence of contract resolvers to be queried for metadata.</param> |
| | | 15 | | /// <returns>A <see cref="IJsonTypeInfoResolver"/> combining results from <paramref name="resolvers"/>.</returns |
| | | 16 | | /// <exception cref="ArgumentException"><paramref name="resolvers"/> is null.</exception> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// The combined resolver will query each of <paramref name="resolvers"/> in the specified order, |
| | | 19 | | /// returning the first result that is non-null. If all <paramref name="resolvers"/> return null, |
| | | 20 | | /// then the combined resolver will also return <see langword="null"/>. |
| | | 21 | | /// |
| | | 22 | | /// Can be used to combine multiple <see cref="JsonSerializerContext"/> sources, |
| | | 23 | | /// which typically define contract metadata for small subsets of types. |
| | | 24 | | /// It can also be used to fall back to <see cref="DefaultJsonTypeInfoResolver"/> wherever necessary. |
| | | 25 | | /// </remarks> |
| | | 26 | | public static IJsonTypeInfoResolver Combine(params IJsonTypeInfoResolver?[] resolvers) |
| | | 27 | | { |
| | | 28 | | ArgumentNullException.ThrowIfNull(resolvers); |
| | | 29 | | |
| | | 30 | | return Combine((ReadOnlySpan<IJsonTypeInfoResolver?>)resolvers); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Combines multiple <see cref="IJsonTypeInfoResolver"/> sources into one. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="resolvers">Sequence of contract resolvers to be queried for metadata.</param> |
| | | 37 | | /// <returns>A <see cref="IJsonTypeInfoResolver"/> combining results from <paramref name="resolvers"/>.</returns |
| | | 38 | | /// <remarks> |
| | | 39 | | /// The combined resolver will query each of <paramref name="resolvers"/> in the specified order, |
| | | 40 | | /// returning the first result that is non-null. If all <paramref name="resolvers"/> return null, |
| | | 41 | | /// then the combined resolver will also return <see langword="null"/>. |
| | | 42 | | /// |
| | | 43 | | /// Can be used to combine multiple <see cref="JsonSerializerContext"/> sources, |
| | | 44 | | /// which typically define contract metadata for small subsets of types. |
| | | 45 | | /// It can also be used to fall back to <see cref="DefaultJsonTypeInfoResolver"/> wherever necessary. |
| | | 46 | | /// </remarks> |
| | | 47 | | public static IJsonTypeInfoResolver Combine(params ReadOnlySpan<IJsonTypeInfoResolver?> resolvers) |
| | | 48 | | { |
| | | 49 | | var resolverChain = new JsonTypeInfoResolverChain(); |
| | | 50 | | foreach (IJsonTypeInfoResolver? resolver in resolvers) |
| | | 51 | | { |
| | | 52 | | resolverChain.AddFlattened(resolver); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | return resolverChain.Count == 1 ? resolverChain[0] : resolverChain; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Creates a resolver applies modifications to the metadata generated by the source <paramref name="resolver"/> |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="resolver">The source resolver generating <see cref="JsonTypeInfo"/> metadata.</param> |
| | | 62 | | /// <param name="modifier">The delegate modifying non-null <see cref="JsonTypeInfo"/> results.</param> |
| | | 63 | | /// <returns>A new <see cref="IJsonTypeInfoResolver"/> instance applying the modifications.</returns> |
| | | 64 | | /// <remarks> |
| | | 65 | | /// This method is closely related to <see cref="DefaultJsonTypeInfoResolver.Modifiers"/> property |
| | | 66 | | /// extended to arbitrary <see cref="IJsonTypeInfoResolver"/> instances. |
| | | 67 | | /// </remarks> |
| | | 68 | | public static IJsonTypeInfoResolver WithAddedModifier(this IJsonTypeInfoResolver resolver, Action<JsonTypeInfo> |
| | | 69 | | { |
| | | 70 | | ArgumentNullException.ThrowIfNull(resolver); |
| | | 71 | | ArgumentNullException.ThrowIfNull(modifier); |
| | | 72 | | |
| | | 73 | | return resolver is JsonTypeInfoResolverWithAddedModifiers resolverWithModifiers |
| | | 74 | | ? resolverWithModifiers.WithAddedModifier(modifier) |
| | | 75 | | : new JsonTypeInfoResolverWithAddedModifiers(resolver, new[] { modifier }); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets a resolver that returns null <see cref="JsonTypeInfo"/> for every type. |
| | | 80 | | /// </summary> |
| | | 81 | | internal static IJsonTypeInfoResolver Empty { get; } = new EmptyJsonTypeInfoResolver(); |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Indicates whether the metadata generated by the current resolver |
| | | 85 | | /// are compatible with the run time specified <see cref="JsonSerializerOptions"/>. |
| | | 86 | | /// </summary> |
| | | 87 | | internal static bool IsCompatibleWithOptions(this IJsonTypeInfoResolver? resolver, JsonSerializerOptions options |
| | | 88 | | => resolver is IBuiltInJsonTypeInfoResolver bir && bir.IsCompatibleWithOptions(options); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// A <see cref="IJsonTypeInfoResolver"/> that returns null for all inputs. |
| | | 93 | | /// </summary> |
| | | 94 | | internal sealed class EmptyJsonTypeInfoResolver : IJsonTypeInfoResolver, IBuiltInJsonTypeInfoResolver |
| | | 95 | | { |
| | 0 | 96 | | public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options) => null; |
| | 0 | 97 | | public bool IsCompatibleWithOptions(JsonSerializerOptions _) => true; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Implemented by the built-in converters to avoid rooting |
| | | 102 | | /// unused resolver dependencies in the context of the trimmer. |
| | | 103 | | /// </summary> |
| | | 104 | | internal interface IBuiltInJsonTypeInfoResolver |
| | | 105 | | { |
| | | 106 | | /// <summary> |
| | | 107 | | /// Indicates whether the metadata generated by the current resolver |
| | | 108 | | /// are compatible with the run time specified <see cref="JsonSerializerOptions"/>. |
| | | 109 | | /// </summary> |
| | | 110 | | bool IsCompatibleWithOptions(JsonSerializerOptions options); |
| | | 111 | | } |
| | | 112 | | } |