| | | 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 | | internal class JsonTypeInfoResolverChain : ConfigurationList<IJsonTypeInfoResolver>, IJsonTypeInfoResolver, IBuiltIn |
| | | 7 | | { |
| | 0 | 8 | | public JsonTypeInfoResolverChain() : base(null) { } |
| | 0 | 9 | | public override bool IsReadOnly => true; |
| | | 10 | | protected override void OnCollectionModifying() |
| | 0 | 11 | | => ThrowHelper.ThrowInvalidOperationException_TypeInfoResolverChainImmutable(); |
| | | 12 | | |
| | | 13 | | public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options) |
| | 0 | 14 | | { |
| | 0 | 15 | | foreach (IJsonTypeInfoResolver resolver in _list) |
| | 0 | 16 | | { |
| | 0 | 17 | | JsonTypeInfo? typeInfo = resolver.GetTypeInfo(type, options); |
| | 0 | 18 | | if (typeInfo != null) |
| | 0 | 19 | | { |
| | 0 | 20 | | return typeInfo; |
| | | 21 | | } |
| | 0 | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | return null; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | internal void AddFlattened(IJsonTypeInfoResolver? resolver) |
| | 0 | 28 | | { |
| | 0 | 29 | | switch (resolver) |
| | | 30 | | { |
| | | 31 | | case null or EmptyJsonTypeInfoResolver: |
| | 0 | 32 | | break; |
| | | 33 | | |
| | | 34 | | case JsonTypeInfoResolverChain otherChain: |
| | 0 | 35 | | _list.AddRange(otherChain); |
| | 0 | 36 | | break; |
| | | 37 | | |
| | | 38 | | default: |
| | 0 | 39 | | _list.Add(resolver); |
| | 0 | 40 | | break; |
| | | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | bool IBuiltInJsonTypeInfoResolver.IsCompatibleWithOptions(JsonSerializerOptions options) |
| | 0 | 45 | | { |
| | 0 | 46 | | foreach (IJsonTypeInfoResolver component in _list) |
| | 0 | 47 | | { |
| | 0 | 48 | | if (!component.IsCompatibleWithOptions(options)) |
| | 0 | 49 | | { |
| | 0 | 50 | | return false; |
| | | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | return true; |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public override string ToString() |
| | 0 | 58 | | { |
| | 0 | 59 | | var sb = new StringBuilder("["); |
| | 0 | 60 | | foreach (IJsonTypeInfoResolver resolver in _list) |
| | 0 | 61 | | { |
| | 0 | 62 | | sb.Append(resolver); |
| | 0 | 63 | | sb.Append(", "); |
| | 0 | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | if (_list.Count > 0) |
| | 0 | 67 | | sb.Length -= 2; |
| | | 68 | | |
| | 0 | 69 | | sb.Append(']'); |
| | 0 | 70 | | return sb.ToString(); |
| | 0 | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |