| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization |
| | | 8 | | { |
| | | 9 | | internal sealed class IgnoreReferenceResolver : ReferenceResolver |
| | | 10 | | { |
| | | 11 | | // The stack of references on the branch of the current object graph, used to detect reference cycles. |
| | | 12 | | private Stack<ReferenceEqualsWrapper>? _stackForCycleDetection; |
| | | 13 | | |
| | | 14 | | internal override void PopReferenceForCycleDetection() |
| | 0 | 15 | | { |
| | 0 | 16 | | Debug.Assert(_stackForCycleDetection != null); |
| | 0 | 17 | | _stackForCycleDetection.Pop(); |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | internal override bool ContainsReferenceForCycleDetection(object value) |
| | 0 | 21 | | => _stackForCycleDetection?.Contains(new ReferenceEqualsWrapper(value)) ?? false; |
| | | 22 | | |
| | | 23 | | internal override void PushReferenceForCycleDetection(object value) |
| | 0 | 24 | | { |
| | 0 | 25 | | var wrappedValue = new ReferenceEqualsWrapper(value); |
| | | 26 | | |
| | 0 | 27 | | _stackForCycleDetection ??= new Stack<ReferenceEqualsWrapper>(); |
| | | 28 | | |
| | 0 | 29 | | Debug.Assert(!_stackForCycleDetection.Contains(wrappedValue)); |
| | 0 | 30 | | _stackForCycleDetection.Push(wrappedValue); |
| | 0 | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | public override void AddReference(string referenceId, object value) => throw new InvalidOperationException(); |
| | | 34 | | |
| | 0 | 35 | | public override string GetReference(object value, out bool alreadyExists) => throw new InvalidOperationException |
| | | 36 | | |
| | 0 | 37 | | public override object ResolveReference(string referenceId) => throw new InvalidOperationException(); |
| | | 38 | | } |
| | | 39 | | } |