| | | 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 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace System.Text.Json.Serialization.Metadata |
| | | 7 | | { |
| | | 8 | | internal sealed class JsonTypeInfoResolverWithAddedModifiers : IJsonTypeInfoResolver |
| | | 9 | | { |
| | | 10 | | private readonly IJsonTypeInfoResolver _source; |
| | | 11 | | private readonly Action<JsonTypeInfo>[] _modifiers; |
| | | 12 | | |
| | 0 | 13 | | public JsonTypeInfoResolverWithAddedModifiers(IJsonTypeInfoResolver source, Action<JsonTypeInfo>[] modifiers) |
| | 0 | 14 | | { |
| | 0 | 15 | | Debug.Assert(modifiers.Length > 0); |
| | 0 | 16 | | _source = source; |
| | 0 | 17 | | _modifiers = modifiers; |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | public JsonTypeInfoResolverWithAddedModifiers WithAddedModifier(Action<JsonTypeInfo> modifier) |
| | 0 | 21 | | { |
| | 0 | 22 | | var newModifiers = new Action<JsonTypeInfo>[_modifiers.Length + 1]; |
| | 0 | 23 | | _modifiers.CopyTo(newModifiers, 0); |
| | 0 | 24 | | newModifiers[_modifiers.Length] = modifier; |
| | | 25 | | |
| | 0 | 26 | | return new JsonTypeInfoResolverWithAddedModifiers(_source, newModifiers); |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options) |
| | 0 | 30 | | { |
| | 0 | 31 | | JsonTypeInfo? typeInfo = _source.GetTypeInfo(type, options); |
| | | 32 | | |
| | 0 | 33 | | if (typeInfo != null) |
| | 0 | 34 | | { |
| | 0 | 35 | | foreach (Action<JsonTypeInfo> modifier in _modifiers) |
| | 0 | 36 | | { |
| | 0 | 37 | | modifier(typeInfo); |
| | 0 | 38 | | } |
| | 0 | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | return typeInfo; |
| | 0 | 42 | | } |
| | | 43 | | } |
| | | 44 | | } |