-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
IgnoreReferenceResolver.cs
42 lines (32 loc) · 1.64 KB
/
IgnoreReferenceResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
namespace System.Text.Json.Serialization
{
internal sealed class IgnoreReferenceResolver : ReferenceResolver
{
// The stack of references on the branch of the current object graph, used to detect reference cycles.
private Stack<ReferenceEqualsWrapper>? _stackForCycleDetection;
internal override void PopReferenceForCycleDetection()
{
Debug.Assert(_stackForCycleDetection != null);
_stackForCycleDetection.Pop();
}
internal override bool ContainsReferenceForCycleDetection(object value)
=> _stackForCycleDetection?.Contains(new ReferenceEqualsWrapper(value)) ?? false;
internal override void PushReferenceForCycleDetection(object value)
{
var wrappedValue = new ReferenceEqualsWrapper(value);
if (_stackForCycleDetection is null)
{
_stackForCycleDetection = new Stack<ReferenceEqualsWrapper>();
}
Debug.Assert(!_stackForCycleDetection.Contains(wrappedValue));
_stackForCycleDetection.Push(wrappedValue);
}
public override void AddReference(string referenceId, object value) => throw new InvalidOperationException();
public override string GetReference(object value, out bool alreadyExists) => throw new InvalidOperationException();
public override object ResolveReference(string referenceId) => throw new InvalidOperationException();
}
}