| | | 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 |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// This class defines how the <see cref="JsonSerializer"/> deals with references on serialization and deserializati |
| | | 8 | | /// Defines the core behavior of preserving references on serialization and deserialization. |
| | | 9 | | /// </summary> |
| | | 10 | | public abstract class ReferenceResolver |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Adds an entry to the bag of references using the specified id and value. |
| | | 14 | | /// This method gets called when an $id metadata property from a JSON object is read. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="referenceId">The identifier of the respective JSON object or array.</param> |
| | | 17 | | /// <param name="value">The value of the respective CLR reference type object that results from parsing the JSON |
| | | 18 | | public abstract void AddReference(string referenceId, object value); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets the reference identifier of the specified value if exists; otherwise a new id is assigned. |
| | | 22 | | /// This method gets called before a CLR object is written so we can decide whether to write $id and enumerate t |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="value">The value of the CLR reference type object to get an id for.</param> |
| | | 25 | | /// <param name="alreadyExists">When this method returns, <see langword="true"/> if a reference to value already |
| | | 26 | | /// <returns>The reference id for the specified object.</returns> |
| | | 27 | | public abstract string GetReference(object value, out bool alreadyExists); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Returns the CLR reference type object related to the specified reference id. |
| | | 31 | | /// This method gets called when $ref metadata property is read. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="referenceId">The reference id related to the returned object.</param> |
| | | 34 | | /// <returns>The reference type object related to specified reference id.</returns> |
| | | 35 | | public abstract object ResolveReference(string referenceId); |
| | | 36 | | |
| | | 37 | | // We are breaking single responsibility on this class internally. |
| | | 38 | | // In the future, if this model is required to be exposed, we can add a base class and extend this class and a n |
| | 0 | 39 | | internal virtual void PopReferenceForCycleDetection() => throw new InvalidOperationException(); |
| | | 40 | | |
| | 0 | 41 | | internal virtual void PushReferenceForCycleDetection(object value) => throw new InvalidOperationException(); |
| | | 42 | | |
| | 0 | 43 | | internal virtual bool ContainsReferenceForCycleDetection(object value) => throw new InvalidOperationException(); |
| | | 44 | | } |
| | | 45 | | } |