2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using System.Collections.Generic;
29
using System.Runtime.CompilerServices;
31
using Newtonsoft.Json.Utilities;
32
using System.Globalization;
34
namespace Newtonsoft.Json.Serialization
36
internal class DefaultReferenceResolver : IReferenceResolver
38
private class ReferenceEqualsEqualityComparer : IEqualityComparer<object>
40
bool IEqualityComparer<object>.Equals(object x, object y)
42
return ReferenceEquals(x, y);
45
int IEqualityComparer<object>.GetHashCode(object obj)
48
// put objects in a bucket based on their reference
49
return RuntimeHelpers.GetHashCode(obj);
51
// put all objects in the same bucket so ReferenceEquals is called on all
57
private int _referenceCount;
58
private BidirectionalDictionary<string, object> _mappings;
60
private BidirectionalDictionary<string, object> Mappings
64
// override equality comparer for object key dictionary
65
// object will be modified as it deserializes and might have mutable hashcode
66
if (_mappings == null)
67
_mappings = new BidirectionalDictionary<string, object>(
68
EqualityComparer<string>.Default,
69
new ReferenceEqualsEqualityComparer());
75
public object ResolveReference(string reference)
78
Mappings.TryGetByFirst(reference, out value);
82
public string GetReference(object value)
85
if (!Mappings.TryGetBySecond(value, out reference))
88
reference = _referenceCount.ToString(CultureInfo.InvariantCulture);
89
Mappings.Add(reference, value);
95
public void AddReference(string reference, object value)
97
Mappings.Add(reference, value);
100
public bool IsReferenced(object value)
103
return Mappings.TryGetBySecond(value, out reference);
2
// Copyright (c) 2007 James Newton-King
4
// Permission is hereby granted, free of charge, to any person
5
// obtaining a copy of this software and associated documentation
6
// files (the "Software"), to deal in the Software without
7
// restriction, including without limitation the rights to use,
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the
10
// Software is furnished to do so, subject to the following
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
// OTHER DEALINGS IN THE SOFTWARE.
27
using Newtonsoft.Json.Utilities;
28
using System.Globalization;
30
namespace Newtonsoft.Json.Serialization
32
internal class DefaultReferenceResolver : IReferenceResolver
34
private int _referenceCount;
36
private BidirectionalDictionary<string, object> GetMappings(object context)
38
JsonSerializerInternalBase internalSerializer;
40
if (context is JsonSerializerInternalBase)
41
internalSerializer = (JsonSerializerInternalBase) context;
42
else if (context is JsonSerializerProxy)
43
internalSerializer = ((JsonSerializerProxy) context).GetInternalSerializer();
45
throw new JsonException("The DefaultReferenceResolver can only be used internally.");
47
return internalSerializer.DefaultReferenceMappings;
50
public object ResolveReference(object context, string reference)
53
GetMappings(context).TryGetByFirst(reference, out value);
57
public string GetReference(object context, object value)
59
var mappings = GetMappings(context);
62
if (!mappings.TryGetBySecond(value, out reference))
65
reference = _referenceCount.ToString(CultureInfo.InvariantCulture);
66
mappings.Add(reference, value);
72
public void AddReference(object context, string reference, object value)
74
GetMappings(context).Add(reference, value);
77
public bool IsReferenced(object context, object value)
80
return GetMappings(context).TryGetBySecond(value, out reference);
b'\\ No newline at end of file'