Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Serialization\DefaultReferenceResolver.cs

Symbol Coverage: 100.00% (21 of 21)

Branch Coverage: 100.00% (12 of 12)

Cyclomatic Complexity Avg: 1.25 Max:2

Code Lines: 23


L V Source
1
#region License
2
// Copyright (c) 2007 James Newton-King
3
//
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
11
// conditions:
12
//
13
// The above copyright notice and this permission notice shall be
14
// included in all copies or substantial portions of the Software.
15
//
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.
24
#endregion
25

  
26
using System;
27
using System.Collections.Generic;
28
using System.Linq;
29
using System.Runtime.CompilerServices;
30
using System.Text;
31
using Newtonsoft.Json.Utilities;
32
using System.Globalization;
33

  
34
namespace Newtonsoft.Json.Serialization
35
{
36
  internal class DefaultReferenceResolver : IReferenceResolver
37
  {
38
    private class ReferenceEqualsEqualityComparer : IEqualityComparer<object>
39
    {
40
      bool IEqualityComparer<object>.Equals(object x, object y)
41
      {
42
 26
        return ReferenceEquals(x, y);
43
 26
      }
44

  
45
      int IEqualityComparer<object>.GetHashCode(object obj)
46
      {
47
#if !PocketPC
48
        // put objects in a bucket based on their reference
49
 166
        return RuntimeHelpers.GetHashCode(obj);
50
#else
51
        // put all objects in the same bucket so ReferenceEquals is called on all
52
        return -1;
53
#endif
54
 166
      }
55
    }
56

  
57
    private int _referenceCount;
58
    private BidirectionalDictionary<string, object> _mappings;
59

  
60
    private BidirectionalDictionary<string, object> Mappings
61
    {
62
      get
63
      {
64
        // override equality comparer for object key dictionary
65
        // object will be modified as it deserializes and might have mutable hashcode
66
 161
        if (_mappings == null)
67
 28
          _mappings = new BidirectionalDictionary<string, object>(
68
 28
            EqualityComparer<string>.Default,
69
 28
            new ReferenceEqualsEqualityComparer());
70

  
71
 161
        return _mappings;
72
 161
      }
73
    }
74

  
75
    public object ResolveReference(string reference)
76
    {
77
      object value;
78
 12
      Mappings.TryGetByFirst(reference, out value);
79
 12
      return value;
80
 12
    }
81

  
82
    public string GetReference(object value)
83
    {
84
      string reference;
85
 46
      if (!Mappings.TryGetBySecond(value, out reference))
86
      {
87
 33
        _referenceCount++;
88
 33
        reference = _referenceCount.ToString(CultureInfo.InvariantCulture);
89
 33
        Mappings.Add(reference, value);
90
      }
91

  
92
 46
      return reference;
93
 46
    }
94

  
95
    public void AddReference(string reference, object value)
96
    {
97
 32
      Mappings.Add(reference, value);
98
 32
    }
99

  
100
    public bool IsReferenced(object value)
101
    {
102
      string reference;
103
 38
      return Mappings.TryGetBySecond(value, out reference);
104
 38
    }
105
  }
106
}